ASP.NET MVC: Get lowercase links (instead of Camel Case)

我怕爱的太早我们不能终老 提交于 2019-12-13 12:30:40

问题


All my dynamically generated action links etc. are creating links like /Account/Setup. It looks strange.

I'd like all my links to be lowercase (meaning /account/setup). Any way to do this?


回答1:


Take a look at http://goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/. You can find more information in another stackoverflow at How can I have lowercase routes in ASP.NET MVC?.

The other posts thus far have not tackled the scenario where you navigate to the root of your web directory. If you have a mapping that directs to the HomeController Index action, you would like the following URL to appear:

mysite/home/ or even mysite/home/index

No amount of Html helper function usage will change the fact that, by default, the following will be shown in the browser location bar:

mysite/Home or mysite/Home/Index




回答2:


There is simpler solution in .NET Framework 4.5, a new property RouteCollection.LowercaseUrls, here's an example




回答3:


Write an extension method for Html:

public static class MyHtmlExtensions
{
    public static string LowerActionLink(this HtmlHelper htmlHelper,someargs)
    {
        return String.ToLowerInvariant(htmlHelper.ActionLink(someArgs));
    }
}

Then use Html.LowerActionLink instead of Html.ActionLink



来源:https://stackoverflow.com/questions/1417389/asp-net-mvc-get-lowercase-links-instead-of-camel-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!