在 ASP.NET MVC 中,如何从 /somepage 重写为 /pages/showpage/somepage?

发布于 2024-08-09 21:35:30 字数 220 浏览 7 评论 0原文

如何将 /SomePage 重写为 /Pages/ShowPage/SomePage 之类的 url?

我尝试过:

            routes.MapRoute("myroute", "{id}", new { controller = "Pages", action = "ShowPage" });

但它不起作用。我做错了什么?

How can I rewrite a url like: /SomePage to /Pages/ShowPage/SomePage?

I tried:

            routes.MapRoute("myroute", "{id}", new { controller = "Pages", action = "ShowPage" });

But It's not working. What am I doing wrong?

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

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

发布评论

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

评论(5

三人与歌 2024-08-16 21:35:30

如果您想说“导航到 /SomePage 应调用 PagesController.ShowPage("SomePage")”,那么您可能想要这样:

// Find a method with signature PagesController.ShowPage( string param )
// and call it as PagesController.ShowPage("SomePage")
route.MapRoute(
    "MyRoute",
    "SomePage",
    new { controller = "Pages", action = "ShowPage", param = "SomePage" } );

这将仅< /em> 重定向确切的 URL /SomePage。如果您想说“导航到 /{something} 应运行 PagesController.ShowPage( some ) 方法”,那么这是一个更困难的问题。

如果第二种情况确实是您想要的,那么您必须在大多数其他路线之后定义它。您想要的路由条目是:

// This will call the method PagesController.ShowPage( string param )
route.MapRoute(
    "MyRoute",
    "{param}",
    new { controller = "Pages", action = "ShowPage" } );

If you are trying to say "navigating to /SomePage shall call PagesController.ShowPage("SomePage")", then you probably want this:

// Find a method with signature PagesController.ShowPage( string param )
// and call it as PagesController.ShowPage("SomePage")
route.MapRoute(
    "MyRoute",
    "SomePage",
    new { controller = "Pages", action = "ShowPage", param = "SomePage" } );

This will only redirect the exact URL /SomePage. If you are trying to say "navigating to /{something} shall run the PagesController.ShowPage( something ) method", then that is a more difficult problem.

If this second case is indeed what you want, then you'll have to define it after most of your other routes. The routing entry you would want would be:

// This will call the method PagesController.ShowPage( string param )
route.MapRoute(
    "MyRoute",
    "{param}",
    new { controller = "Pages", action = "ShowPage" } );
影子的影子 2024-08-16 21:35:30

我认为这应该是:

routes.MapRoute("myroute", "{controller}/{action}/{id}", new { controller = "Pages", action = "ShowPage", id = "SomePage" });

I think this should be:

routes.MapRoute("myroute", "{controller}/{action}/{id}", new { controller = "Pages", action = "ShowPage", id = "SomePage" });
ゞ花落谁相伴 2024-08-16 21:35:30

这是错误的,因为我认为在你的应用程序中,有一个默认的地图路由:

routes.MapRoute(
            "root",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

它将查找名称等于你传入的 id 的控制器,如果你删除这个默认的地图路由,你的地图路由就会工作。

您应该尝试这个路由调试器工具,它有很大帮助:

调试器工具

It was wrong because I think in your application, there is this default map route :

routes.MapRoute(
            "root",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

it will looks for the controller with the name equal to the id that you passed in, if you remove this default map route, your map route would work.

You should try this route debugger tool, it helps out a lot:

Debugger Tool

寄居人 2024-08-16 21:35:30

您需要确保您的路线映射到您的对象。在您的情况下,您需要有一个名为 PagesController 的控制器,其中包含一个名为 ShowPage 的方法,以及一个名为 pagename 的参数(如果您使用如下所示的路由)。

routes.MapRoute("route", "{controller}/{action}/{pagename}", new { controller = "Pages", action = "ShowPage", pagename = "" }  );

另外,不要忘记在指定路由时可以使用正则表达式 - 这可以帮助您确保路由引擎使用正确的路由。

routes.Add(new Route("{controller}/{action}/{params}",
    new RouteValueDictionary { { "controller", "user" }, { "action", "login" }, { "params", "" } },
    new RouteValueDictionary { { "controller", @"^(?!Resources)\w*$" }, { "action", "[a-zA-Z]+" } },
    new MvcRouteHandler()));

You need to ensure that your route maps to your objects. In your case, you need to have a controller called PagesController with a method called ShowPage, with a single parameter called pagename (if you use a route like the following).

routes.MapRoute("route", "{controller}/{action}/{pagename}", new { controller = "Pages", action = "ShowPage", pagename = "" }  );

Also, do not forget that you can use regex when specifying the route - this may help you ensure the correct route is used by the routing engine.

routes.Add(new Route("{controller}/{action}/{params}",
    new RouteValueDictionary { { "controller", "user" }, { "action", "login" }, { "params", "" } },
    new RouteValueDictionary { { "controller", @"^(?!Resources)\w*$" }, { "action", "[a-zA-Z]+" } },
    new MvcRouteHandler()));
倾城月光淡如水﹏ 2024-08-16 21:35:30

您可以编写自己的静态路由。在默认路由上方添加您自己的路由。

routes.MapRoute("MyRoute",                                              // Route name            
    "Pages/ShowPage/SomePage/{id}",                           // URL with parameters            
    new { controller = "Pages", action = "ShowPage", id = "" }  // Parameter defaults        
);

现在,如果 SomePage 是一个变量,您将需要这样的内容:

routes.MapRoute("MyRoute",                                              // Route name            
    "Pages/ShowPage/{somePage}/{id}",                           // URL with parameters            
    new { controller = "Pages", action = "ShowPage", id = "", somePage = "" }  // Parameter defaults        
);

如果需要,您可以省略 {id},只需将其保留在操作参数之外即可。

routes.MapRoute("MyRoute",                                              // Route name            
    "Pages/ShowPage/{somePage}",                           // URL with parameters            
    new { controller = "Pages", action = "ShowPage", somePage = "" }  // Parameter defaults        
);

You can write your own static routing. Above the default route add your own.

routes.MapRoute("MyRoute",                                              // Route name            
    "Pages/ShowPage/SomePage/{id}",                           // URL with parameters            
    new { controller = "Pages", action = "ShowPage", id = "" }  // Parameter defaults        
);

Now, if SomePage is a variable, you'll want something like this:

routes.MapRoute("MyRoute",                                              // Route name            
    "Pages/ShowPage/{somePage}/{id}",                           // URL with parameters            
    new { controller = "Pages", action = "ShowPage", id = "", somePage = "" }  // Parameter defaults        
);

You can leave out the {id} if you want, just leave it out of your action parameters.

routes.MapRoute("MyRoute",                                              // Route name            
    "Pages/ShowPage/{somePage}",                           // URL with parameters            
    new { controller = "Pages", action = "ShowPage", somePage = "" }  // Parameter defaults        
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文