【发布时间】:2016-07-30 08:00:58
【问题描述】:
首先我要显示代码
routes.MapRoute(
name: "SubCategory",
url: "Category/{categoryName}/{subName}",
defaults: new { controller = "Categories", action = "SubCategory", categoryName = "", subName = "" }
);
这是我的路线 categoryName 和 subName 是变量
// GET: Category/{categoryName}/{subName}
public ActionResult SubCategory(string categoryName, string subName)
{
CategoriesViewResult viewResult = new CategoriesViewResult();
viewResult.Categories = _db.Categories.ToList();
viewResult.CurrentSubCategory = _db.SubCategories.First(x => x.Category.CategoryName == categoryName && x.SubCategoryName == subName);
return View(viewResult);
}
这是我的方法; 但我得到404。 我应该如何写我的路线。
更新
这是高于默认路线的。
【问题讨论】:
-
route.config 中的顺序很重要。这是高于还是低于默认路由?
-
CategoriesController中有那个方法吗? -
确定在 CategoriesController 中
-
您使用的是什么网址?很可能,由于您没有使用
UrlParameter.Optional,因此您的网址实际上缺少{categoryName}和{subName}的值。 -
这段代码对我来说看起来不错,它应该适用于
yourSite/Category/books/drama
标签: c# asp.net .net asp.net-mvc routing