MVC: Multiple Routes for 1 Controller - asp.net-mvc

I want users to be able to access the "/Linecard" page of my ASP.Net MVC site using "/Linecard" or "/Manufacturers" as the URL... so same controller, 2 different possible URLs.
I tried adding the following:
routes.MapRoute(
name: "Manufacturers",
url: "Manufacturers/{action}/{id}",
defaults: new { controller = "Linecard", action = "Index", id = UrlParameter.Optional }
);
Adding this after the "Default" route doesn't work at all and I get a 404 error when I go to "/Manufacturers". Putting it BEFORE "Default" works, but then only "/Manufacturers" shows up in the URL when I click menu links since it is the first match. I would like "/Linecard" to always show as the URL.
Any pointers? Is there a certain constraint I can use to accomplish this? Thanks!

I had the same problem when we moved to extension-less URLs. We needed to continue to support one route with extensions. I got around it by having my default route apply to everything except the old URL, then after that mapping one specifically for the exception
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
// if controller specified does not match 'manufacturers' (case insensitive)
new { controller = "^((?i)(?!manufacturers).)*$" },
new string[] { "Namespace.Of.Controllers" }
);
routes.MapRoute(
"Manufacturers", // Route name
"Manufacturers/{action}/{id}", // URL with parameters
new { controller = "Linecard", action = "Index", id = UrlParameter.Optional },
new string[] { "Namespace.Of.Controllers" }
);

You could also set an order when mapping your routes with the defaults at the end like so
routes.MapRoute(
name: "Manufacturers",
url: "Manufacturers/{action}/{id}",
defaults: new { controller = "Linecard", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Related

Why is this MVC route incorrect?

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

URL not getting read correctly by MVC 4 routing

Hi I have a URL as follows in my MVC 4 application
http://localhost/ABC/Home/DeleteApp/3000
and I have configured RouteConfig.cs as follows
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
It goes to the Correct DeleteApp Controller but with the ID value Null. It shows the correct URL when I enable firebug.
What could possibly go wrong?
it debugs and come to here but the id field as null;
public void DeleteApp(string id)
{
try
{
// delete logic
}
catch (Exception e)
{
//
}
}
Possibles causes :
-> Check your "id" variable name (must be exactly the same name in the RouteConfig and in the controller action, it is case sensitive)
-> You have other routes mapped before this one that match the pattern
If you have a route mapped as {controller}/{action}/{something} and another route mapped as {controller}/{action}/{something_else}, when you call the url "Home/DeleteApp/3000", it can't tell if 3000 is "something" or "something_else" so it will take the first match.
To make this working, you have to use "Route Constraints"
http://www.codeproject.com/Articles/641783/Customizing-Routes-in-ASP-NET-MVC
You need to do it like so:
routes.MapRoute(
name: "ABC",
url: "ABC/{controller}/{action}/{id}",
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 }
);
If you want to have url in this format http://localhost/ABC then you will have to modify the route as below by adding ABC to {controller}/{action}/{id} expression.
routes.MapRoute(
name: "Default",
url: "ABC/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Wrong MVC route matched. Asp.net

This is my route registration code:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"course_list",
"course/list",
new { controller = "course", action = "list" }
);
routes.MapRoute(
"course_view",
"course/view/{id}",
new { controller = "course", action = "list", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I have a link /course/view/87
And the route that is matched is /course/list
Can anyone explain why?
Thank you
UPDATE:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"course_list",
"course/list",
new { controller = "course", action = "list" }
);
routes.MapRoute(
"course_view",
"course/view/{id}",
new { controller = "course", action = "view", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But I'm still getting the same issue.
When i visit: /course/view/87 i get a 404 error.
It appears that your route for course/view/{Id} has a 'list' action. I expect this is a typo.
Adding these routes to an empty Asp.Net Mvc 4 project and using routedebugger (http://nuget.org/packages/routedebugger/), I get a Matched Route of "course/view/{id}". You should use routedebugger locally to see what is going on. The above code seems to be fine.
The button element is treated as submit button (i.e.: type="submit" if not default type attribute is set). Therefore, the browser initiated a post request, which no route satisfied, since all my actions are get(s).
Thank you all for your time.

Route value removed but still added as query string parameter

I am extending a model to include a link property. It is based on other properties within it to generate a link:
public partial class MyModelType
{
public string FilterLink
{
get
{
var u = new UrlHelper(HttpContext.Current.Request.RequestContext);
var route = new RouteValueDictionary(u.RequestContext.RouteData.Values);
route.Remove("other");
route.Add("other", null);
route.Add("id", this.Id);
return u.Action("Index", "ControllerName", route);
}
}
}
The link generated is /ControllerName/1?other=2. If I get rid of the route.Remove("other") I get the link based on the mapped route: /ControllerName/1/2. How can I prevent it being used as a query string parameter when it is removed from the route? The UrlHelper seems to be adding it somehow, even though it is not a route value.
Check your routing configuration.
MVCs routing will append any unkown parts of your route in that way, so as long as you define it up front it should work just fine.
You probably just have this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Try adding the option like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{option}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, option = UrlParameter.Optional }
);
The issue is discussed here: http://erraticdev.blogspot.com/2011/03/removing-route-values-from-linksurls-in.html
However, the blogs proposal is convoluted compared with a simple solution in the comments: add "other = null as object" to the defaults values on your DEFAULT route.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, other = null as object }
);

How to choose route in MVC3

I have global.ascx with three routes
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"TestRoute",
"{id}",
new { controller = "Product", action = "Index3", id = UrlParameter.Optional },
new { id = #"\d+" } //one or more digits only, no alphabetical characters
);
routes.MapRoute(
"TestCatalogRoute",
"{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional } // Parameter defaults
//new { controller = "Product", action = "Index2", id = UrlParameter.Optional } // Parameter defaults
);
}
When I enter url:
http://mydomain.com/
It uses "TestCatalogRoute" route, but I want "Default" route T.T
How to:
with url: http://mydomain.com it uses "Default" route
with url: http://mydomain.com/1 it uses "TestRoute" route (It's already done!)
with url: http://mydomain.com/abc it uses "TestCatalogRoute" route
Remove id = UrlParameter.Optional for TestCatalogRoute then
Change the order of your routes. The routehandler will validate each route, first one that matches will get picked. So if you put the second one last, you should be fine?
I can recomend using Routing Debugger to debug your route easy to use.
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

Resources