I'm trying to add a route to the default one, so that I have both urls working:
http://www.mywebsite.com/users/create
http://www.mywebsite.com/users/1
This will make the first route work:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "users", action = "Index", id = UrlParameter.Optional }
);
However, the second route won't work obviously.
This will make the second route work, but will break the first one:
routes.MapRoute(
name: "Book",
url: "books/{id}",
defaults: new { controller = "users", action = "Details" }
);
How to combine the two route configurations so that both URLs work?
I apologize if there is already a question like this on SO, I wasn't able to find anything.
The key is to put more specific routes first. So put the "Book" route first. Edit I guess you also need a constraint to only allow numbers to match the "id" part of this route. End edit
routes.MapRoute(
name: "Book",
url: "books/{id}",
defaults: new { controller = "users", action = "Details" },
constraints: new { id = #"\d+" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "users", action = "Index", id = UrlParameter.Optional }
);
And ensure that the "id" parameter in your "Details" action is an int:
// "users" controller
public ActionResult books(int id)
{
// ...
}
This way, the "Books" route will not catch a URL like /users/create (since the second parameter is reqiured to be a number), and so will fall through to the next ("Default") route.
Related
My requirement is I need to set multiple routes to same controller/action method.
If user enters url http://localhost:xxxx/home/index , it will target "index" action method of "home" controller.
I also want "http://localhost:xxxx/products" and "http://localhost:xxxx/categories" to point to "index" action method of "home" controller.
I was able to achive this by adding two routes "categories" and "products" as mentioned below , and it is working fine.
routes.MapRoute(
name: "categories",
url: "categories",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "products",
url: "products",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
My question is , is there any way I combine those two routes "categories" and "products" in to one ?
You can achieve this by adding a constraint to your route.
Make the entire path a parameter, and then assign a regular expression rule to match this parameter.
routes.MapRoute(
name: "IndexMapper",
url: "{alternateIndexName}",
defaults: new { controller="Home", action="Index" },
constraints: new { alternateIndexName="(categories)|(products)"}
);
https://msdn.microsoft.com/en-us/library/cc668201.aspx#Anchor_6
In my MVC application's RouteConfig I am using this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute (
name: "Dictionary", // Route name
url: "dictionary/{id}", // URL with parameters
defaults: new { controller = "Dictionary", action = "details" } // Defaults
);
At this time this URL succeeds in returning a page:
http://127.0.0.1:8080/dictionary/details/1
Whereas this fails:
http://127.0.0.1:8080/dictionary/1
With the exception
"HTTP 404.The resource you are looking for (or one of its dependencies) could have been removed, had its name changed..."
When I swap the routing around, like this:
routes.MapRoute (
name: "Dictionary", // Route name
url: "dictionary/{id}", // URL with parameters
defaults: new { controller = "Dictionary", action = "details" } // Defaults
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
It both URLs start returning a page.
Why is this?
Thanks in advance.
FYI This question is different to the possible duplicate because of the UrlParameter.Optional and the wild carded routing in the other question. This question is more about which specific ordering is correct and why.
The order of the routes that you add to the route table is important. If you reversed the order, then the Default route always will get called instead of the custom route.
Reference:
https://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/creating-custom-routes-cs
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
);
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 am trying to understand routes, but am very confused.
The question is simply, does the default values have any impact on which router is chosen, or is it simply the pattern.
For example, consider the following
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{startIndex}",
defaults: new { controller = "Home", action = "Index", startIndex = UrlParameter.Optional }
);
routes.MapRoute(
name: "About",
url: "{controller}/{action}/{startIndex}",
defaults: new { controller = "About", action = "Index", startIndex = UrlParameter.Optional }
);
Regardless of whether the end result is the same, would MVC simply choose the first every time since the URL: pattern matches the request, and therefore ignore that they have different controllers?
Route selection uses pattern matching and will select the first matching pattern. In your example, there's no reason for your second route as the first will match /about/... as well as the second and result in the same action being invoked. If you need to have routes which have the same basic pattern, perhaps the values in the pattern affect the controller you use, you can use routing constraints to aid in choosing the correct route or use fixed values and place the route before the default route.
routes.MapRoute(
name: "Contact",
url: "/contact",
defaults: new { controller = "about", action = "contactus", id = "" }
);
routes.MapRoute(
name: "Help",
url: "/help",
defaults: new { controller = "about", action = "help", id = "" }
);
routes.MapRoute(
name: "Admin",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Admin", action = "Index", id= UrlParameter.Optional },
constraints: new { controller = "(admin)|(orgadmin)" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{startIndex}",
defaults: new { controller = "Home", action = "Index", startIndex = UrlParameter.Optional }
);