【问题标题】:ASP.NET MVC Routing two parameters with static string in betweenASP.NET MVC 路由两个参数,中间有静态字符串
【发布时间】:2010-01-16 01:42:44
【问题描述】:

这是我的路线:

routes.MapRoute(null,
    "shelves/{id1}/products/{action}/{id2}",
    new { controller = "Products", action = "List", id1 = "", id2 = ""});

这个想法是你可以做这样的事情:

http://server/shelves/23/products/edit/14

并且能够编辑货架 23 上的产品 14。使用 Route Debugger 检查它,路径与路由匹配,但是当我尝试在关闭 Route Debugger 的情况下导航到它时,它向我显示 HTTP 404 错误。有人知道为什么会这样吗?

【问题讨论】:

  • 请添加您的 ProductsController 的代码及其操作方法。

标签: asp.net-mvc routing routetable


【解决方案1】:

嗯,对于初学者来说,id1="" 行会有问题,因为你不能把结尾没有的东西做成可选的。

我刚刚在我的系统上试了一下,效果很好。

这是路线:

routes.MapRoute(
    "shelf-route",
    "shelves/{id1}/products/{action}/{id2}",
    new { controller = "Products", action = "List", id2 = "" }
);

这是控制器:

public class ProductsController : Controller
{
    public string List(string id1, string id2)
    {
        return String.Format("ID1 = {0}, ID2 = {1}", id1, id2);
    }
}

我试过这样的网址:

http://localhost:14314/shelves/23/products/list/14
http://localhost:14314/shelves/23/products

他们工作得很好。

当您尝试包含“编辑”的 URL 时,您是否记得进行了编辑操作?如果没有编辑操作,您将收到 404。

【讨论】:

  • 感谢您的回答。事实证明,我的路线以错误的顺序列出,所以它首先匹配另一条路线。我应该仔细看看 Route Debugger 的输出。
猜你喜欢
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多