【问题标题】:Asp Mvc 4 action was not found未找到 Asp Mvc 4 操作
【发布时间】: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


【解决方案1】:

正如 Tieson T. 在评论中指出的那样,添加路线的顺序很重要。第一个匹配的路由将被映射到处理程序。

定义路线可能会令人困惑且难以诊断。我发现以下提示对处理路线很有用:

  1. 调试路由:您可以按照here 的说明使用route debugger
  2. 在 RouteConfig 文件中定义尽可能少的路由。

一旦您获得了有关哪个 URL 映射到哪个路由的更多信息,就更容易弄清楚如何调整 RouteConfig.cs 文件中的顺序。如果您仍然遇到问题,请共享以相同顺序定义的所有路由并共享哪个 URL 有问题。

【讨论】:

  • 路由调试器在我的机器上不工作。当我在浏览器中运行网站时,它会显示主页。在 web.config 中也启用了路由调试器。你能详细解释一下我怎样才能让它工作或任何详细的例子吗?
  • 你看过这个链接itorian.com/2013/10/debugging-mvc-with-route-debugger-tool.html吗?我用过一次,我所做的就是安装 nuget 包。
  • @Anjum 您在使用路由调试器时遇到了什么错误?您必须在 URL 中输入路线才能正常工作..
【解决方案2】:

添加默认动作路由:

    routes.MapRoute(
        name: "DefaultAction",
        url: "{controller}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

对于管理区域:

        routes.MapRoute(
            name: "DefaultId",
            url: "Admin/{controller}/{action}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

【讨论】:

    【解决方案3】:

    我找不到原因。所以现在要完成我的项目,我将所有控制器从管理区域移动到根目录。这对我有用。 但问题仍然存在,为什么会发生这种情况以及如何解决。

    【讨论】:

    • 如果问题没有得到回答,您应该将其保持打开状态,而不是将其标记为正确答案。
    猜你喜欢
    • 2012-02-20
    • 2013-04-07
    • 2015-12-27
    • 2016-04-08
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多