In MVC5 routing I need one situation. I have 2 namespaces with same controller name. I want to dynamically chose between controllers without conflict
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Namespace1","Namespace2" });
In here if there are 2 controllers with same name and actions under both namespaces it gives conflict error. What I want to do is prioritize namespaces.
If both namespaces has Home.Index action I want Namespace1 take the lead instead of error.
If namespace1 does not have the action I want system check for Namespace2.
I tried few ways but I don't want to use attribute routing better architecture.
We have a BaseECommerce asp.net mvc project and UI projects that are taking inherit of BaseEcommerce project. Purpose is if there is same Controller and Action in UI project use that. Else use base project.
Thanks for help.
You prioritize routes by registering them in the correct order - the first match wins.
You cannot prioritize namespaces. The namespaces field is for specifying all of the namespaces for a specific route (which is usually a single namespace). These are the set of namespaces where the specific route will scan for controllers.
So, if you want to avoid a conflict between similar named controllers, you need to have a route for each namespace. But, since the first matching route always wins, for it to work each controller action will need a unique URL.
routes.MapRoute(
"Default", // Route name
"Namespace2/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Namespace2.Controllers" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Namespace1.Controllers" });
There are other ways of making routes match requests than above - for ideas, see this answer.
Note that you could use a custom route constraint to check whether some module is loaded into your application and if so, disable the route.
public class ModuleNotInstalledConstraint : IRouteConstraint
{
public bool Match(
HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection)
{
if (module is installed)
{
return false;
}
return true;
}
}
Usage
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { _ = new ModuleNotInstalledConstraint() },
new[] { "Namespace1.Controllers" });
This way, the route is ignored if the module is available, so you can use a route with the exact same signature and it will be "overridden" when the module is available. But you will still need to specify namespaces to ensure the scan doesn't conflict with controllers that have the same name.
Related
In an ASP.NET MVC 5 web application, there is a RouteConfig class that registers many routes. In all of the examples I have seen so far, only the "Default" route has a non-empty name. The URL pattern and default route values seem to be sufficient to correctly associate any URL to the controller and action to execute. So, is there any benefit to naming a route? For debugging or logging? Just for self-documenting the code?
For example:
public class RouteConfig
{
public static void RegisterRoutes( RouteCollection routes )
{
routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );
// Most pages.
routes.MapRoute( name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "home", action = "index", id = UrlParameter.Optional }
// A special page.
// Should the route have a name?
routes.MapRoute( name: "",
url: "special",
defaults: new { controller = "special", action = "index", HttpVerbs = HttpVerbs.Get } );
}
}
The main reason to name a route is so that if you need a link to a route from somewhere in the app, you can reference the name. That way if the actual URL/path changes you don't break everywhere in the code that references that route.
Problem
For the default route MVC is returning a "Multiple types were found that match the controller named 'admin'." error instead of a 404 not found. There is no admin controller in that namespace.
We're using MVC 5.2.2.
Background
We're using MVC areas. Two areas contain an "admin" controller. When you use the full path as defined in their respective routes, both areas' admin controllers are accessible and work correctly.
The problem arises when you try to access "admin" from the default route. Admin does not exist in that context, so we'd expect a 404 not found, however instead we receive:
Multiple types were found that match the controller named 'admin'. This can happen
if the route that services this request ('{controller}/{action}/{id}') does not
specify namespaces to search for a controller that matches the request. If this
is the case, register this route by calling an overload of the 'MapRoute' method
that takes a 'namespaces' parameter.
The request for 'admin' has found the following matching controllers:
DMP.Stock.Web.Controllers.AdminController
DMP.Inventory.Web.Controllers.AdminController
Here is our default route and two area routes:
public static void RegisterRoutes(RouteCollection routes)
{
// Default Route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "DMP.Core.Web.Controllers" }
);
}
public override void RegisterArea(AreaRegistrationContext context)
{
// Area 1: Stock
context.MapRoute(
name: "Stock_default",
url: "Stock/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "DMP.Stock.Web.Controllers" }
);
}
public override void RegisterArea(AreaRegistrationContext context)
{
// Area 2: Inventory
context.MapRoute(
"Inventory_default",
"Inventory/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "DMP.Inventory.Web.Controllers" }
);
}
/Stock/Admin/Index works correctly.
/Inventory/Admin/Index works correctly.
/Admin/ does not work correctly (expect: 404 not found, receive "multiple controllers" error).
The errors suggests we add namespaces to our routes, but as you can see above both the default and the two areas already have a namespace definition. The default route points at a namespace without any "admin" controllers in it.
I think MVC is trying to be "helpful" by searching for possible controllers that could match the requested URL. Is there any way I can turn that off?
I've been able to resolve this issue myself. Here is the solution I found:
// Default Route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "DMP.Core.Web.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;
Note the addition of .DataTokens["UseNamespaceFallback"] = false; this is what resolves the issue. There isn't very much (any?) documentation on this functionality, however I found it while reading the MVC source code, specifically within DefaultControllerfactory (the source of this issue).
After you know to google for "UseNamespaceFallback" you can find a few blog posts and questions where people have had a similar issue and resolved it the same way. However I can find no MSDN documentation on this DataToken.
I have myself a little problem with areas and controllers.
I have a controller called "HomeController" this is the default controller, I then added a new area called "Supplier", I then copied over my HomeController and removed all the methods I don't want to edit and just edited my Index method.
Now when I build it works fine but when I go to my home controller as a supplier it comes up with this error
Multiple types were found that match the controller named 'Home'. This can
happen if the route that services this request ('{controller}/{action}/{id}')
does not specify namespaces to search for a controller that matches the request.
If this is the case, register this route by calling an overload of the
'MapRoute' method that takes a 'namespaces' parameter.
The request for 'Home' has found the following matching controllers:
TestProject.Controllers.Home
TestProject.Areas.Supplier.Controllers.Home
I have updated my areas like so
This is my default area
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "TestProject.Controllers" }
);
}
}
And here is my area route file
public class SupplierAreaRegistration: AreaRegistration
{
public override string AreaName
{
get
{
return "Supplier";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"SupplierHomeIndex",
"Home/Index/{id}",
defaults: new { area = "Supplier", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "TestProject.Areas.Supplier.Controllers" },
constraints: new { permalink = new AreaConstraint(AreaName) }
);
context.MapRoute(
"SupplierDefault",
"{controller}/{action}/{id}",
defaults: new { area = "Supplier", action = "index", id = UrlParameter.Optional },
namespaces: new[] { "TestProject.Controllers"},
constraints: new { permalink = new AreaConstraint(AreaName) }
);
}
}
Can anyone sign some light on this? I have looked at many topics and answers for this via Google and Stackoverflow however nothing seems to work for me.
You've customized the area's routes and removed the Supplier URL prefix. When the routing framework spins up it merely collects all controllers from your application, regardless of where they are, and then looks for a match based on the URL. In your case, you now have two controllers that are both bound to the URL /Home/*. Typically, the area's URL would be prefixed with the area's name to avoid the confusion, i.e. /Supplier/Home.
I am trying to set up custom routing with the following mapped route
edit: my full route config
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
#region FixtureAdmin
routes.MapRoute(
name: "FixtureEdit",
url: "{controller}/{action}/{id}",
defaults: new { controller = "FixtureAdmin", action = "Edit", id = UrlParameter.Optional }
);
#endregion
#region Results
routes.MapRoute(
name: "ResultAdd",
url: "{controller}/{action}/{fixtureId}",
defaults: new { controller = "Result", action = "Add", fixtureId = UrlParameter.Optional }
);
#endregion
And my controller code
public ActionResult Add(int fixtureId)
{
// return model to view etc..
}
This is coming up with the exception, even though I have specified the parameter as optional.
The parameters dictionary contains a null entry for parameter 'fixtureId'
The strange thing is, if I change the parameter of the Add action to just 'Id' then the following URL will work Result/Add/1. I'm confused, is there some default routing that is overriding my custom one? Why would changing the parameter to just 'Id' work?
Edit
Just to test, I added another parameter to the action
public ActionResult Add(int? fixtureId, int? testId)
I then edited the route accordingly and now it works, so I reckon it is an issue with default routing.
Use a nullable int in your Controller Action.
public ActionResult Add(int? fixtureId)
{
// return model to view etc..
}
But the question is, if that is indeed an ID, how would it react if a null/blank ID is requested by the user? Is that ID a key in your DB? You can make it nullable if you are able to handle or provide a default page if the ID is blank/null.
EDIT:
This is because ASP.NET will assume that an unidentified parameter in your request is the id, in your case, Results/Add/1, 1 is unidentified. If you want to make that code work with using fixtureId, you should use Results/Add?fixureId=1. So ultimately, it's not because of the routing, but instead, it's because of the parameter in the Action that you have.
EDIT2:
Also, what you are experiencing there is called a routing conflict. Your routes are conflicting with the Default. You can try to apply constraints.
2,
from your post i think your problem is putting your custom route after default, like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
name: "ResultAdd",
url: "{controller}/{action}/{fixtureId}",
defaults: new { controller = "Home", action = "Add", fixtureId = UrlParameter.Optional }
so:
1/ exception "The parameters dictionary contains a null entry for parameter 'fixtureId'" will come if you dont give the specific route name for any action link or route form because MVC will use default route to routing. So you need to give specific route name to your custom route can be worked, like this:
#Html.ActionLink("Test custom Route", "Add", "Home", new { fixtureId = 1 }, "ResultAdd")
Cheer
Look at this method of adding what the developer calls a 'NullableConstraint' clicky link So if the optional parameter is supplied you can do some checking on it's value.
And also look at the answer following the accepted answer for what seems a simpler, cleaner solution.
In my MVC application I have a default route defined:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new []{ "Demo.Controllers" }
);
I created a new Area called Admin and it added a route to in the AdminAreaRegistration class:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", controller = "Home", id = UrlParameter.Optional }
);
}
In my main _Layout file I tried to do the following:
#Html.RouteLink("Admin", "Admin_default")
It only works in certain cases (such as if I'm already in an Admin page). If I am in the /Home/About section of my site, then the URL gets generated like so:
/Admin/Home/About
If I am in my Index action of the Home controller (in the main area, not admin) then the URL gets generated like so:
/Admin
Why doesn't RouteLink work like I think it should using Areas in MVC?
#Html.RouteLink("Admin", "Admin_default") this route uses the route with the name of Admin_default so it will always use that route to generate the url.
#Html.RouteLink("Admin", "Admin_default", new { controller = "Home", action = "Index" })
When you don't specify stuff like route values most of MVC uses the values that currently exist. In this case since the action and controller values are null it looks at the RouteValues and checks to see if they're there and if they're found they use the values found there instead.
This is sometimes helpful. For instance if you want {id} to be populated with the same value that was used on the page.
Url: /home/edit/1701
#Html.RouteLink("Refresh", "Default") would result in the same exact url - you can specify overrides if you want.
I would also suggest not naming your routes. You can force yourself to do this by passing null to the route name arg when creating it.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(null,
"Admin/{controller}/{action}/{id}",
new { action = "Index", controller = "Home", id = UrlParameter.Optional }
);
}
You can then use RouteLink like so:
#Html.RouteLink("Admin",
new { controller = "Home", action = "Index", area = "Admin" })
Reply to comment
Wish I could say I came up with the idea. Learned this from Steve Sanderson's MVC book. There are a ton of method overloads that take a route name as a parameter (RouteLink, but also RedirectToRoute and others). Not using route names forces you to use other overloads of those methods, which IMO are more intuitive and easier to "get right".
Naming your routes is not recommended since it creates dependencies in your view towards the existing routes. I suggest using a null value