【发布时间】:2014-12-16 05:17:58
【问题描述】:
我正在使用 ASP MVC 4 开发一个项目。它运行良好。但几天前我遇到了一个非常奇怪的问题。 当我将其索引称为 ~/Dashboard/index 时,我有控制器“仪表板”,它工作正常,但如果我将其称为 ~/Dashboard,我得到 HTTP 404 not found 错误。
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "UI.Web.Controllers" }
);
我什至为仪表板控制器添加了一个路由配置,但它不起作用
routes.MapRoute(
name: "Dashboard",
url: "Dashboard/{action}/{id}",
defaults: new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "UI.Web.Controllers" }
);
在我的管理区域中的某些控制器中出现了同样的问题(有些工作正常,有些给出 HTTP 404 not found 错误。 我的区域路线配置是:
context.MapRoute(
"DMS_Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Contact", action = "Index", id = UrlParameter.Optional },
new string[] { "UI.Web.Areas.Admin.Controllers" }
);
在管理区域中,一些控制器的索引操作不起作用,而一个控制器的 AddEdit 操作也不起作用。
public ActionResult AddEdit(string id)
{
var user = new Models.RegisterModel();//string.IsNullOrWhiteSpace(id) ? new Models.RegisterModel() : Membership.GetUser(id);
if (!string.IsNullOrWhiteSpace(id))
{
var obj = Membership.GetUser(id);
if (obj != null)
{
user.Email = obj.Email;
user.UserName = obj.UserName;
}
}
ViewBag.PageTitle = string.IsNullOrWhiteSpace(id) ? "Add User" : "Edit User";
ViewBag.IsAjax = Request.IsAjaxRequest();
if (Request.IsAjaxRequest())
return PartialView("~/Areas/Admin/Views/Users/AddEditPartial.cshtml", user);
return View("~/Areas/Admin/Views/Users/AddEdit.cshtml", user);
}
在 AddEdit 操作中,如果我将其称为 ~/Admin/User/AddEdit/a 它可以工作,但如果我将其称为 ~/Admin/User/AddEdit 它也会给出 HTTP 404 not found 错误。
这种行为是由于某些框架更新问题或代码问题造成的吗?任何想法.... 我不认为这是编码问题,因为之前使用相同的代码......
现在我即将完成我的项目,在最后阶段我被困在这里。
完整的 routes.config 如下所示:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Dashboard",
url: "Dashboard/{action}/{id}",
defaults: new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "UI.Web.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "UI.Web.Controllers" }
);
我的 AdminAreaRegistration 代码是:
context.MapRoute(
"DMS_Users",
"Admin/Users/{action}/{id}",
new { controller = "Users", action = "Index", id = UrlParameter.Optional },
new string[] { "UI.Web.Areas.Admin.Controllers" }
);
context.MapRoute(
"DMS_Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Contact", action = "Index", id = UrlParameter.Optional },
new string[] { "UI.Web.Areas.Admin.Controllers" }
);
【问题讨论】:
-
添加路由的顺序很重要;请编辑您的问题以显示出现在您的 RouteConfig 文件中的路线。
标签: asp.net asp.net-mvc asp.net-mvc-4 http-status-code-404 url-routing