如何获得斜线附加到页面的路线? [英] How to get a trailing slash appended to page routes?

查看:133
本文介绍了如何获得斜线附加到页面的路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个几乎相同的问题,我不得不说这个查询的,尽管我的是一个Web表单应用场景(.NET 4中使用的路由),而不是MVC的。

This is a near identical problem I am having to that of this query, albeit mine is a Web Forms scenario (using routing in .NET 4) as opposed to MVC.

添加斜线在每个网址的结尾?

这是有人提到有一半提供不幸为纽带,以完整的解决方案,该解决方案被打破了。

The solution that someone mentions there is only half provided unfortunately as the link to the complete solution is broken.

目前,从我的网页航线任何尾随斜线时,我得到的路线网址被删除。 这是特别有问题时,我想使用下列类型的内联语法对我的Web表单:

At the moment, any trailing slash from my page routes is removed when I get the route url. This is especially problematic when I want to use the following type of inline syntax on my web form:

<a runat="server" href='<%$RouteUrl:RouteName=Posts %>'>

在这里再次最后的斜线被删除,尽管它是present在我的路由表。

Again here the trailing slash is removed, despite it being present in my route table.

任何人都可以请帮助提供清洁,高效地解决这个问题?理想的情况下,像在其他堆栈溢出线程提供的接近完成的解决方案我已经把上面的?

Can anyone please help provide a clean, efficient solution to this problem? Ideally, like the 'nearly complete' solution provided in the other Stack Overflow thread I've put above?

推荐答案

我现在已经找到了解决方案,我原来的问题。我发现下面的文章,其中介绍了如何确保您所有的网址是小写,所以我只是用这个例子code,但附加斜线在那里执行ToLowerInvariant()。

I have now found the solution to my original question. I found the following article which describes how you can ensure all of your Url's are in lowercase, so I just used this example code, but appended a trailing slash where it performs the ToLowerInvariant().

所以,我的2个辅助类现在看起来是这样的:

So, my 2 helper classes now look like this:

public class LowercaseRoute : System.Web.Routing.Route
{
    public LowercaseRoute(string url, IRouteHandler routeHandler)
        : base(url, routeHandler) { }
    public LowercaseRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
        : base(url, defaults, routeHandler) { }
    public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
        : base(url, defaults, constraints, routeHandler) { }
    public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler)
        : base(url, defaults, constraints, dataTokens, routeHandler) { }

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

        if (path != null)
            path.VirtualPath = path.VirtualPath.ToLowerInvariant() + "/";

        return path;
    }
}

public static class RouteCollectionExtensions
{
    public static void MapRouteLowercase(this RouteCollection routes, string name, string url, object defaults)
    {
        routes.MapRouteLowercase(name, url, defaults, null);
    }

    public static void MapRouteLowercase(this RouteCollection routes, string name, string url, object defaults, object constraints)
    {
        if (routes == null)
            throw new ArgumentNullException("routes");

        if (url == null)
            throw new ArgumentNullException("url");

        var route = new LowercaseRoute(url, new MvcRouteHandler())
        {
            Defaults = new RouteValueDictionary(defaults),
            Constraints = new RouteValueDictionary(constraints)
        };

        if (String.IsNullOrEmpty(name))
            routes.Add(route);
        else
            routes.Add(name, route);
    }
}

在我的Global.asax,而不是使用图路线'我的RegisterRoutes内常规,我叫我的新MapRouteLowercase方法,而不是(传递相同的参数),例如:

Within my global.asax, instead of using 'MapRoute' within my RegisterRoutes routine, I call my new MapRouteLowercase method instead (passing in the same parameters), e.g.

routes.MapRouteLowercase("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });

这是我从这里可以发现这个code原文章:

The original article that I got this code from can be found here:

http://goneale.com/2008 / 12/19 /小写路由-网址功能于ASPNET-MVC /

这篇关于如何获得斜线附加到页面的路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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