【问题标题】:Why am I getting 404 when the route matches? ASP.Net MVC为什么当路由匹配时我得到 404? ASP.Net MVC
【发布时间】:2011-08-26 03:07:41
【问题描述】:

我已经在这个问题上纠结了几个小时了。

我有一个名为“DecisionPoint”的控制器,我在它的“ApplicationState”操作上设置了一个断点。无论我尝试什么,我都会在浏览器中不断收到 404。我怀疑我的路由不正确,所以我下载了一个路由调试器,它使我们尝试的 URL 与控制器和操作相匹配。那么为什么我得到一个 404 却看不到断点命中呢?

/DecisionPoint/ApplicationState/no/worky --> 404

控制器:

 public ActionResult ApplicationState(string fileName, string stateString)
        {
            string filePath = GetDpFilePath(fileName);
            HtmlDocument htmlDocument = new HtmlDocument();
            htmlDocument.Load(filePath);
            HtmlNode stateScriptNode =
                htmlDocument.DocumentNode.SelectSingleNode("/html/head/script[@id ='applicationState']");
            stateScriptNode.InnerHtml = "var applicationStateJSON =" + stateString;
            htmlDocument.Save(filePath);

            return Json("State Updated");

路线

 routes.MapRoute(
        "DecisionPointState", // Route name
        "DecisionPoint/ApplicationState/{fileName}/{stateString}", // URL with parameters
        new {controller = "DecisionPoint", action = "ApplicationState"} // Parameter defaults
    );


        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );


        }`
**Update**

我创建了一个全新的控制器并且它可以工作。这就是我的路由表现在的样子。状态控制器正确路由到 SaveState

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
           "StateRoute", // Route name
           "State/SaveState/{file}/{state}", // URL with parameters
           new { controller = "State", action = "SaveState", file = UrlParameter.Optional, state = UrlParameter.Optional } // Parameter defaults
       );

        routes.MapRoute(
           "DPStateRoute", // Route name
           "DecisionPoint/ApplicationState/{file}/{state}", // URL with parameters
           new { controller = "DecisionPoint", action = "ApplicationState", file = UrlParameter.Optional, state = UrlParameter.Optional } // Parameter defaults
       );


        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
       // RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);

    }
}

所以我被难住了..

【问题讨论】:

  • 嗯...我看不出有什么特别的错误:/
  • 您可以在该控制器中执行任何其他操作吗?在那里抛出一个索引动作,看看你是否击中了控制器。你有控制器构造函数吗?在那里放置一个断点,看看它是否达到了这个目标。
  • 请出示控制器代码。

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


【解决方案1】:

确保您的控制器类名为 DecisionPointController 而不是 DecisionPoint

【讨论】:

    【解决方案2】:

    如果您的 AreaRegistration 类和 Controller 类不在同一个命名空间中,您将处于这种情况 - 路由将匹配(并且 RouteDebugger 将确认这一点)但您会在“正确”的网址。

    解决方案是确保两个类在同一个命名空间中。

    【讨论】:

    • 还要确保控制器类的名称正确。我花了2个小时调试,因为文件名正确,但类名不正确
    • RouteDebugger 链接已失效。如果可以,请更新?
    【解决方案3】:

    这可能是由于您的 Controller 位于另一个引用不同版本的 System.Web.Mvc 的项目中!

    我有同样的症状 - 找到了正确的路线,但得到了 404!在 HttpHandler.ProcessRequest 上,抛出了一个异常,说处理程序的控制器没有实现 IController

    【讨论】:

    • 你如何使用另一个 dll 中的控制器?
    • @jjxtra 在回答时,我认为您只需将不同的命名空间放在 MapRoute 助手中。现在有了 ASP.NET Core,AddApplicationPart 扩展需要一些额外的东西
    【解决方案4】:

    我最近遇到了同样的问题。对我来说,问题是如果不同的命名空间(一个在一个区域中),我有两个同名的控制器。

    【讨论】:

      【解决方案5】:

      愚蠢,但它让我: 确保您的控制器继承自 Controller

      【讨论】:

        【解决方案6】:

        我不是路由专家,但我总是为默认参数添加所有参数:

        routes.MapRoute(
            "DecisionPointState", // Route name
            "DecisionPoint/ApplicationState/{fileName}/{stateString}", // URL with parameters
            new {controller = "DecisionPoint", 
                 action = "ApplicationState"
                 fileName = UrlParameter.Optional,
                 stateString = UrlParameter.Optional
             } // Parameter defaults
        );
        

        【讨论】:

        • 我已经尝试过了。在这种情况下,我提供了两个参数。谢谢
        • Nick,我知道这听起来有点疯狂,但是如果你将 Action 重命名为其他名称会发生​​什么?我在想ApplicationState 这个名字会让 IIS 感到困惑。
        • 我刚刚尝试将其更改为“UpdateState”,但它仍然给出 404
        【解决方案7】:

        在我的例子中,同样问题的答案是需要“包含在项目中”相关的控制器和视图,而不是不正确的路由规则。

        创建我的时,由于某种原因它们没有自动包含在内。这个问题是在我关闭并重新打开解决方案后才发现的。

        {+1 hat} 授予 Visual Studio,因为它有缺陷的超自动化让我挖掘 Web.Config 文件,尝试添加扩展,甚至尝试(但失败)打造一个像样的 ErrorController。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-25
          • 2016-02-01
          • 1970-01-01
          相关资源
          最近更新 更多