url 中没有控制器的 MVC 路由

发布于 2024-11-08 06:11:52 字数 519 浏览 5 评论 0原文

如何配置 ASP.NET MVC 3 路由,使其不在 url 中显示控制器?

这是我

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

        routes.MapRoute(
            "HomeActions", 
            "{action}", 
            new { action= "AboutUs" } 
        );

需要网址的路线:

mysite.com/AboutUs

但我有

 mysite.com/Home/AboutUs

How do I configure ASP.NET MVC 3 routing so it doesn’t shown the controller in the url?

Here's my routes

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

        routes.MapRoute(
            "HomeActions", 
            "{action}", 
            new { action= "AboutUs" } 
        );

I need url:

mysite.com/AboutUs

But I have

 mysite.com/Home/AboutUs

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

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

发布评论

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

评论(2

贩梦商人 2024-11-15 06:11:52

我会非常具体地说明您要路由的网址。并将其放置在默认路由之上。

    routes.MapRoute(
        "HomeActions", 
        "AboutUs", 
        new { controller = "Home", action= "AboutUs" } 
    );

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

对您建议的路线不太具体可能会产生不良后果。特别是如果在默认路由下面列出。

routes.MapRoute(
    "HomeActions", 
    "{action}", 
    new { controller = "Home", action= "AboutUs" } 
);

例如,如果将上述路由添加到默认路由之后,则 url http://www.example.com/AboutUs< /a> 可能会匹配路由 {controller = "AboutUs", action = "Index", id = UrlParamter.Optional}。如果您在默认路由之上添加了路由,则查找 url http://www.example.com/Users< /a> 您可能希望作为用户控制器上的索引操作现在将在主控制器上查找用户操作。

因此,我建议对此类路线进行具体说明。

I would be very specific about the url that you want to route. And place it above the default route.

    routes.MapRoute(
        "HomeActions", 
        "AboutUs", 
        new { controller = "Home", action= "AboutUs" } 
    );

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

Being less specific with a route like the one you suggested might have unwanted consequences. Especially if listed below the default route.

routes.MapRoute(
    "HomeActions", 
    "{action}", 
    new { controller = "Home", action= "AboutUs" } 
);

For example, if the above route is added after the default then the url http://www.example.com/AboutUs would likely match the route {controller = "AboutUs", action = "Index", id = UrlParamter.Optional}. If you added the route above the default one, then looking for the url http://www.example.com/Users which you might want to be the Index action on the Users controller would now look for the Users action on the Home controller.

So, I would advise being specific about routes like that.

-柠檬树下少年和吉他 2024-11-15 06:11:52

您需要添加不带 {controller} 部分的路由,并在 defaults 参数中指定控制器名称。

You need to add a route without the {controller} portion, and specify the controller name in the defaults parameter.

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