在 ASP.NET MVC 中,如何从 /somepage 重写为 /pages/showpage/somepage?
如何将 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想说“导航到
/SomePage
应调用PagesController.ShowPage("SomePage")
”,那么您可能想要这样:这将仅< /em> 重定向确切的 URL
/SomePage
。如果您想说“导航到/{something}
应运行PagesController.ShowPage( some )
方法”,那么这是一个更困难的问题。如果第二种情况确实是您想要的,那么您必须在大多数其他路线之后定义它。您想要的路由条目是:
If you are trying to say "navigating to
/SomePage
shall callPagesController.ShowPage("SomePage")
", then you probably want this:This will only redirect the exact URL
/SomePage
. If you are trying to say "navigating to/{something}
shall run thePagesController.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:
我认为这应该是:
I think this should be:
这是错误的,因为我认为在你的应用程序中,有一个默认的地图路由:
它将查找名称等于你传入的 id 的控制器,如果你删除这个默认的地图路由,你的地图路由就会工作。
您应该尝试这个路由调试器工具,它有很大帮助:
调试器工具
It was wrong because I think in your application, there is this default map route :
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
您需要确保您的路线映射到您的对象。在您的情况下,您需要有一个名为 PagesController 的控制器,其中包含一个名为 ShowPage 的方法,以及一个名为 pagename 的参数(如果您使用如下所示的路由)。
另外,不要忘记在指定路由时可以使用正则表达式 - 这可以帮助您确保路由引擎使用正确的路由。
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).
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.
您可以编写自己的静态路由。在默认路由上方添加您自己的路由。
现在,如果 SomePage 是一个变量,您将需要这样的内容:
如果需要,您可以省略 {id},只需将其保留在操作参数之外即可。
You can write your own static routing. Above the default route add your own.
Now, if SomePage is a variable, you'll want something like this:
You can leave out the {id} if you want, just leave it out of your action parameters.