ASP.NET MVC 路由两个参数之间有静态字符串
这是我的路由:
routes.MapRoute(null,
"shelves/{id1}/products/{action}/{id2}",
new { controller = "Products", action = "List", id1 = "", id2 = ""});
想法是您可以执行以下操作:
http://server/shelves/23/products/edit/14
并且能够编辑货架 23 上的产品 14。使用 路由调试器,路径与路由匹配,但是当我尝试在路由调试器关闭的情况下导航到它时,它显示 HTTP 404 错误。有谁知道为什么会发生这种情况?
This is my routing:
routes.MapRoute(null,
"shelves/{id1}/products/{action}/{id2}",
new { controller = "Products", action = "List", id1 = "", id2 = ""});
The thought is that you can do something like this:
http://server/shelves/23/products/edit/14
And be able to edit product 14 on shelf 23. Checking it with Route Debugger, the path matches the routing, but when I try to navigate to it with Route Debugger off, it shows me a HTTP 404 error. Does anybody know why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,对于初学者来说, id1="" 行将会有问题,因为你不能让一些不在最后的可选内容。
我刚刚在我的系统上尝试过,效果很好。
这是路线:
这是控制器:
我尝试了如下 URL:
http://localhost :14314/shelves/23/products/list/14
http://localhost:14314/shelves/23/products
他们工作得很好。
当您尝试使用其中包含“编辑”的 URL 时,您是否记得执行“编辑”操作?如果没有编辑操作,您将收到 404。
Well, for starters, that id1="" line is going to be problematic, because you can't make something optional that's not at the end.
I just tried it on my system, and it works just fine.
This is the route:
This is the controller:
I tried URLs like:
http://localhost:14314/shelves/23/products/list/14
http://localhost:14314/shelves/23/products
And they worked just fine.
When you tried the URL with "edit" in it, did you remember to make an Edit action? If there's no Edit action, you'll get a 404.