Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing ActionLinks to be rendered as lowercase

Without creating my own ActionLink HtmlHelper is there a way to force any ActionLinks to be rendered lowercase?

Update: Check out the following links for extending the RouteCollection to add LowecaseRoutes [http://www.makiwa.com/index.php/2008/05/31/lowercase-mvc-route-urls/] [http://goneale.wordpress.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/]

Update - 02/03/2011: Since the two links above now nolonger work, i'd made a post a while back with my solution

http://blog.lukesmith.net/2009/02/01/generating-and-enforcing-that-any-link-and-request-is-lowercase-with-aspnet-mvc/

like image 354
Luke Smith Avatar asked Jan 30 '09 09:01

Luke Smith


People also ask

How do you force lowercase in HTML?

Using STYLE="text-transform:lowercase" 'Hullo, Mole!'

How can I have lowercase routes in ASP NET MVC?

Solution : LowercaseUrls property on the RouteCollection class has introduced in ASP . NET 4.5. Using this property we can lowercase all of our URLs.


1 Answers

The best way to handle this, is at the routing level. Force all route paths to be lowercase, and it will properly propagate to your action links etc.

The way I've solved this, is to create a new route class that inherits Route and simply overrides the GetVirtualPath method;

public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
    var virtualPath = base.GetVirtualPath(requestContext, values);

    if (virtualPath != null)
        virtualPath.VirtualPath = virtualPath.VirtualPath.ToLowerInvariant();

    return virtualPath;
}

I've also created a few extension methods for RouteCollection to make it easy to use this new route class.

like image 99
thomasjo Avatar answered Sep 20 '22 21:09

thomasjo