【问题标题】:ASP.NET MVC Routing 404 errorASP.NET MVC 路由 404 错误
【发布时间】: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


【解决方案1】:

这样试试

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 2017-03-25
    • 2010-10-07
    • 1970-01-01
    • 2014-10-16
    相关资源
    最近更新 更多