asp.net mvc 路由和神秘的 404

发布于 2024-10-12 08:50:35 字数 780 浏览 3 评论 0原文

我是 .net mvc 的新手。 简而言之,我想查看我的网站,以便人们可以输入: 我的网站/约翰@埃里克 并由正确的控制器进行处理。

另一方面,我希望能够指定直接操作,例如: mywebsite/GetPeople

并通过 GetPeople 操作进行处理。

我在我的应用程序中设置了两条路由规则:

第一条路线

    routes.MapRoute("Default",
                    "{id}",
            new { controller = "Friends", action = "Index", id = UrlParameter.Optional },
            new { controller = @"[^\.]*", id = @"(?i)[a-z]*@[a-z]*" }
        );

第二条路线

    routes.MapRoute(
        "Friends",
        "{action}/{id}",
        new { controller = "Friends" }
        );

第一条和默认路线效果很好,但是当我发送如下请求时: mywebsite/GetPeople cheacky 索引操作接管并读取 GetPeople,就好像它是一个参数一样。尽管我添加了我真正很棒的regax,但这似乎不起作用。

有什么想法吗?

I am new to .net mvc.
In a nutshell, I want to see my website so people can type:
mywebsite/John@Eric
and get processed by the correct controller.

On the other hand, I'd like to be able to also specify direct actions such as:
mywebsite/GetPeople

and get proccessed by GetPeople action.

I have set up two routing rules in my application:

First Route

    routes.MapRoute("Default",
                    "{id}",
            new { controller = "Friends", action = "Index", id = UrlParameter.Optional },
            new { controller = @"[^\.]*", id = @"(?i)[a-z]*@[a-z]*" }
        );

Second Route

    routes.MapRoute(
        "Friends",
        "{action}/{id}",
        new { controller = "Friends" }
        );

The first and default route works great, but then when I send a request like this: mywebsite/GetPeople
the cheacky index action takes over and reads GetPeople as if it were a parameter. Even though I added my real awesome regax, this doesn't seem to work.

Any ideas ?

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

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

发布评论

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

评论(2

颜漓半夏 2024-10-19 08:50:35

您的路线可能如下所示:

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

    routes.MapRoute(
        "Friends",
        "{id}",
        new { controller = "Friends", action = "Index", id = UrlParameter.Optional },
        new { id = @"(?i)[a-z]*@[a-z]*" }
    );

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

现在 mywebsite/John@Eric 将由 Friends 控制器和 mywebsite 的 Index 操作处理/GetPeople 将由 Friends 控制器的 GetFriends 操作处理。

Here's how your routes might look:

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

    routes.MapRoute(
        "Friends",
        "{id}",
        new { controller = "Friends", action = "Index", id = UrlParameter.Optional },
        new { id = @"(?i)[a-z]*@[a-z]*" }
    );

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

Now mywebsite/John@Eric will be handled by the Index action of the Friends controller and mywebsite/GetPeople will be handled by the GetFriends action of the Friends controller.

廻憶裏菂餘溫 2024-10-19 08:50:35

这是因为 MVC 中路由的工作方式决定的。它只是按照 RegisterRoutes 中声明路由的顺序将传入 URL 与路由进行匹配。在这种情况下,URL 中的 GetPeople 将与 Id 参数匹配,因为所有内容都是可选的。为了解决这个问题,我将添加默认值作为最后一条路由。可以这样做

routes.MapRoute("", "{controller}/{action}", new { controller = "Friends",action="Index" });

这将处理 GetMyPeople URL。不过,您需要对其进行索引操作。 MvcContrib 有一个出色的测试助手,用于在从应用程序实际运行 MVC 路由之前对其进行测试。您可以在此处获取这些信息

That is because of the way routing works in MVC. It just matches incoming URLs with routes in the order the routes are declared in RegisterRoutes. In this case the GetPeople in the URL would match with the Id parameter as everything is optional. To fix this, I would add a default as the last route. It could be done as so

routes.MapRoute("", "{controller}/{action}", new { controller = "Friends",action="Index" });

This would handle the GetMyPeople URL. You would need to have an Index action on it though. The MvcContrib has an excellent Test helper for testing out MVC routes before actually running it from the app. You can get the bits here

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