强制ActionLinks呈现为小写 [英] Forcing ActionLinks to be rendered as lowercase

查看:137
本文介绍了强制ActionLinks呈现为小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不创造我自己的ActionLink HtmlHelper的是有没有办法强迫任何ActionLinks要呈现小写?

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

更新: 看看下面的链接扩展RouteCollection添加LowecaseRoutes <打击> [http://www.makiwa.com/index.php/2008/05/31/lowercase-mvc-route-urls/] <打击> [HTTP://goneale.word$p$pss.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/]

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/]

更新 - 02/03/2011: 由于上面现在nolonger工作的两个环节,我发了一个帖子而回我的解决方案

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

<一个href="http://blog.lukesmith.net/2009/02/01/generating-and-enforcing-that-any-link-and-request-is-lowercase-with-aspnet-mvc/" rel="nofollow">http://blog.lukesmith.net/2009/02/01/generating-and-enforcing-that-any-link-and-request-is-lowercase-with-aspnet-mvc/

推荐答案

要处理这种情况最好的办法,就是在路由层面。强制所有路由路径为小写,并且会正确传播到你的行动链接等。

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.

我已经解决这个问题的方法,就是创建一个继承路线,简单地覆盖 GetVirtualPath 方法;

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;
}

我还创建了 RouteCollection 的几个扩展方法,可以很容易地使用这个新的路由类。

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

这篇关于强制ActionLinks呈现为小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆