Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Custom Routes and ActionLinks

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!

like image 375
Thad Avatar asked Jan 18 '10 20:01

Thad


People also ask

What is custom route in MVC?

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.

What is difference between attribute and conventional routing in MVC?

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.

Can we have multiple routes in MVC?

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.


1 Answers

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" })%>
like image 176
Bryan Avatar answered Oct 01 '22 13:10

Bryan