I need this url http://localhost:53249/Admin/EditPosts/1/Edit but unfortunately i m getting query string in url like this http://localhost:53249/Admin/EditPosts?id=1&operation=Edit
This is my actionlink(anchor tag)
<td>#Html.ActionLink(#posts.Title, "EditPosts", "Admin", new { id = posts.id, operation="Detail" }, null)</td>
This is my route config:
public static void RegisterRoutes(RouteCollection routes)
{
//routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Admin",
url: "Admin/EditPosts/{id}/{operation}",
defaults: new { controller = "Admin", action = "EditPosts"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
In this case you are mixing Action and Operation.
I suggest that you use the url like this:
http://localhost:53249/Admin/Posts/1/Edit
Because this way you already indicate your action, Edit, for the Posts objects, and Edit is the action, not an operation, following standard REST.
To use the url suggested you must change the MapRoute to:
routes.MapRoute(
name: "Admin",
url: "Admin/Posts/{id}/{action}",
defaults: new { controller = "Posts", action = "Details" } //Details action by default
);
Related
I am creating a friendly URL so i have crete route in routeconfig file as follows
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ShowUser", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
and action link as follows
View
But it is not working giving runtime error "The resource cannot be found."
Default route and you custom route have identical structure, so default one will always be used because it is declared first.
Im guessing you are getting error because in code you are referring to ShowUser, but instead you should use name of the controller: UserGroupEdit.
View
If you want a friendly route you can do something like this
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ShowUser", // Route name
url: "ShowUser/index/{id}", // URL with parameters
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
}
in your cshtml
#Url.Action( "Index", "UserGroupEdit")
You can use UrlHelper.RouteUrl helper method which allows you to specify the route name:
View
RouteConfig:
routes.MapRoute(
name: "ShowUser",
url: "ShowUser/{action}/{id}",
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional }
);
Reference link
I have created multiple routes with different route name in MVC.
routes.MapRoute(
name: "PostDetails",
url: "Ad/{id}/{item}",
defaults: new { controller = "Home", action = "Post" }
);
I am calling route from a javascript function to redirect to this route
var url = '#Url.RouteUrl("PostDetails", new { id = "_id_", item = "_name_" })';
url = url.replace("_id_", id).replace("_name_", name);
window.location.href = url;
This is giving an error with 404.
You must add your route before Default route in the RouteConfig like:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "PostDetails",
url: "Ad/{id}/{item}",
defaults: new { controller = "Home", action = "Post" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
I am trying to redirect all urls that don't match an existing controller to a certain controller.
For example, the url mywebsite.com/newyork should be processed as mywebsite.com/Cities/Info/newyork
I am using the following code in my RegisterRoutes but it doesn't seem to work as I get a 404 reponse:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Cities",
url: "{cityname}",
defaults: new { controller = "Cities", action = "Info", cityname= "" }
);
You should put your cities route first and drop the empty default parameter:
routes.MapRoute(
name: "Cities",
url: "{cityname}",
defaults: new { controller = "Cities", action = "Info" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The routes are processed in order so you should have most specific first to least specific ( your default route).
As your website.com/newyork matched the default route, it wasn't continuing to your city route.
I have home controller and Index action method. The below url works
http://localhost/home/index
Will it be possible to make it work like below
http://localhost/index.aspx
I am trying below code in Global.asax but does not works
routes.MapPageRoute("MyPage", "create.aspx", "~/home/create");
Route Config
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("MyPage", "create.aspx", "~/home/create");
routes.MapRoute(
name: "Customized",
url: "{action}",
defaults: new { controller = "Home", action = "Default", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Reports",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Reports", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Default", id = UrlParameter.Optional }
);
}
You should use MapRoute() instead of MapPageRoute(), as you are still referring to an MVC controller/action:
routes.MapRoute(
name: "Default2",
url: "index.aspx",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
PS: Remember to register the new route before other ones which may eventually interfere with it.
You can't MapPageRoute for non static content as you are doing. If you wan't to hide the Controller for your route the Customized already do this. If you are mixing MVC + WebForms you should fallow this guide to se how config your routes.
How to: Define Routes for Web Forms Applications
Can I set up a route that will get mapped from a root-level URL like this?
http://localhost:49658/
I'm using the VS2010 built-in web server.
Attempting to set up a route with a blank or a single-slash URL string doesn't work:
routes.MapRoute(
"Default",
"/",
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
It results in the error "The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.". Thanks in advance! My entire route definition is here:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"EditingTitles", // Route name
"{controller}/{action}/{startingLetter}", // URL with parameters
new { controller = "Admin", action = "Index", startingLetter = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
What are you trying to achieve here... a URL that looks like this? http://www.acme.com/ ? Because if you are, the default route will achieve that when none of the parameters are specified.
// Default Route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = String.Empty } // Parameter defaults
);
Using ASPNET MVC5:
RouteConfig.cs file:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Homepage",
url: "",
defaults: new { controller = "Content", action = "Index" }
);
routes.MapRoute(
name: "foo",
url: "bar",
defaults: new { controller = "Content", action = "Index" }
);
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{title}",
defaults: new { controller = "Content", action = "Details", title = UrlParameter.Optional }
);
}
Plus:
If you wishes to redirect your homepage to another route automatically, like "http://www.yoursite.com/" to "http://www.yoursite.com/bar", just use the method RedirectToRoute():
public class ContentController : Controller
{
public ActionResult Index()
{
return RedirectToRoute("foo");
}
}