We are working with an asp.net mvc app that is shared by multiple clients. We need the urls to include the clients url friendly name. For example:
domain.com/clientName/controller/action/id
This below appears to be working when it comes to routing, but the "clientName" is not generated correctly for the action link helpers.
_routes.MapRoute("DefaultRoute",
"{clientName}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = string.Empty },
new { clientName = @"[\w-]+" });
We would like to continue using the Html.ActionLink Helper methods, but it does not include the clientName in the generated link. Do we have to write our own helpers in this scenario or is there an alternative approach?
Has anyone else built an application with this type of routing scenario? Any suggestions would be appreciated!
In the custom routing mode MVC sites respond to incoming requests using standard ASP.NET routing. Page URLs are determined by the routes that you register into your MVC application's routing table.
As per our opinion, Attribute Routing provides us more flexibilities as compared to Convention Based Routing. In Attribute Routing, we can manage route as controller level, action level and also area level. Also, it can override the child route, if required.
Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.
It's been my experience that the ActionLink method referenced by Darin will generate a URL like:
http://host/Home/action?clientName=someClient
If you want to generate the URL exactly as you specified. Check out the RouteLink method that lets you specify the name of the Route you are matching:
<%= Html.RouteLink("some text", "DefaultRoute", new { clientName = "someclient" })%>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With