Asp.net mvc 路由在通配符末尾删除数字文件夹

发布于 2024-09-05 12:05:48 字数 371 浏览 2 评论 0原文

因此,我们有一个路由设置,在末尾有一个通配符来捕获文件路径,该路由可能如下所示:

/{browserName}/{browserVersion}/{locale}/{*packageName}

当我们尝试路径如:

/FF/3/en-US/scripts/packages/6/super.js

最终传递给控制器​​的 packageName 是:

/scripts/packages/super.js

使用路由测试程序也会发生这种情况我们完全不明白这是为什么。如果您用字符串替换 6,它会起作用,如果您在 6 之前添加另一个数字文件夹,它就会被包含在内,因此如果最后一个文件夹是数字,它看起来就会被删除。有谁知道这是为什么吗?

So we have a route setup that has a wildcard at the end to capture a file path, the route might look like:

/{browserName}/{browserVersion}/{locale}/{*packageName}

The problem comes in when we try a path like:

/FF/3/en-US/scripts/packages/6/super.js

What ends up getting passed to the controller as packageName is:

/scripts/packages/super.js

Using the route tester program this also happens so we're at a total loss of why this is. If you replace the 6 with a string, it works, if you add another numeric folder before the 6 it does get included so it appears to just drop if the last folder is numeric. Anyone know why this is?

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

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

发布评论

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

评论(1

荭秂 2024-09-12 12:05:48

我在VS2008中创建了默认的asp.net mvc2项目,并更改了以下代码:
在 global.asax.cs 中,我有以下代码:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "test", // Route name
            "{browserName}/{browserVersion}/{locale}/{*packageName}",
            new { controller = "Test", action = "Index", browserName = "IE", browserVersion = "8", locale = "en-US" , packageName = UrlParameter.Optional } // Parameter defaults
        );
    }

接下来我添加了一个 TestController:

public class TestController : Controller
{
    public ActionResult Index(
        string browserName, 
        string browserVersion, 
        string locale, 
        string packageName)
    {
        return View();
    }
}

和一个空的索引视图:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>


指数

索引

为了方便起见,我在 site.master 中为您指定的 url 添加了一个链接:

<li><a href="/FF/3/en-US/scripts/packages/6/super.js">Test</a></li>

接下来,我在 TestController 的 Index 操作中设置了一个断点。
当我将鼠标悬停在 packageName 参数上时,我看到“scripts/packages/6/super.js”,

所以我无法重现您得到的行为。

您使用的是VS2008和MVC2还是其他版本?

I created the default asp.net mvc2 project in VS2008 and changed the following code:
In the global.asax.cs I have this code:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "test", // Route name
            "{browserName}/{browserVersion}/{locale}/{*packageName}",
            new { controller = "Test", action = "Index", browserName = "IE", browserVersion = "8", locale = "en-US" , packageName = UrlParameter.Optional } // Parameter defaults
        );
    }

And next I added a TestController:

public class TestController : Controller
{
    public ActionResult Index(
        string browserName, 
        string browserVersion, 
        string locale, 
        string packageName)
    {
        return View();
    }
}

And a empty Index View:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
</asp:Content>

for convenience I added a link in the site.master for the url you specified:

<li><a href="/FF/3/en-US/scripts/packages/6/super.js">Test</a></li>

Next I set a breakpoint in the Index action of the TestController.
When I hover over the packageName parameter I see "scripts/packages/6/super.js"

So I can't reproduce the behavior you got.

Are you using VS2008 and MVC2 or other versions?

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