如何将尾部斜杠附加到页面路由?

发布于 2024-10-02 02:54:08 字数 490 浏览 0 评论 0原文

这与我在该查询中遇到的问题几乎相同,尽管我的是 Web 表单场景(使用 .NET 4 中的路由)而不是 MVC。

在每个网址末尾添加尾部斜杠?

有人提到的解决方案只提供了一半,不幸的是作为完整链接的链接解决方案已损坏。

目前,当我获取路由 url 时,页面路由中的任何尾部斜杠都会被删除。 当我想在 Web 表单上使用以下类型的内联语法时,这尤其成问题:

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

这里的尾部斜杠再次被删除,尽管它存在于我的路由表中。

任何人都可以帮助提供一个干净、有效的解决方案来解决这个问题吗?理想情况下,就像我在上面放置的其他 Stack Overflow 线程中提供的“几乎完整”的解决方案一样?

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.

Add a trailing slash at the end of each url?

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

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 %>'>

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就像说晚安 2024-10-09 02:54:08

我现在已经找到了我原来问题的解决方案。我发现下面的文章描述了如何确保所有 Url 均为小写,因此我只使用了此示例代码,但在执行 ToLowerInvariant() 的位置附加了一个尾部斜杠。

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

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 例程中使用“MapRoute”,而是调用我的新 MapRouteLowercase 方法(传递相同的参数),例如

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

我得到的原始文章此代码可以在这里找到:

http:// goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/

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().

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

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

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

http://goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/

叫思念不要吵 2024-10-09 02:54:08

为了使生成的网址始终小写并带有最后一个斜杠,我将 TidyRouting 库添加到我的解决方案中,然后我只是用新的“MapTidyRoute”替换“MapRoute”方法。

to make the generated urls always lowercase and with a final trailing slash I add the TidyRouting library to my solution, then I simply replace the "MapRoute" method with the new "MapTidyRoute".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文