ASP.NET MVC路由404错误

时间:2016-04-08 22:02:36

标签: c# asp.net .net asp.net-mvc routing

首先我要显示代码

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。 我该怎么写我的路线。

更新

这高于默认路线。

1 个答案:

答案 0 :(得分:0)

尝试这样

routes.MapRoute(
            "SubCategory",
            "Category/Sub/{categoryName}/{subName}",
            new { controller = "Categories", action = "SubCategory", apiId = UrlParameter.Optional  }
        );

此外,您使用的是哪个版本的MVC?