为什么我在路线匹配时得到404?ASP.Net MVC

Nic*_*ick 11 asp.net asp.net-mvc asp.net-mvc-routing

我已经被困在这个问题上几个小时了.

我有一个名为'DecisionPoint'的控制器,我在它的'ApplicationState'动作上设置了一个断点.无论我尝试什么,我都会在浏览器中获得404.我怀疑我的路线不正确所以我下载了一个路由调试器,它将我正在尝试的URL与Controller和动作相匹配.那么为什么我会得到一个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");
Run Code Online (Sandbox Code Playgroud)

路线

 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**
Run Code Online (Sandbox Code Playgroud)

我创建了一个全新的控制器,它的工作原理.这是我的路线表的样子.状态控制器correclty路由到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);

    }
}
Run Code Online (Sandbox Code Playgroud)

所以我很难过..

Tre*_*oll 12

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


jam*_*nto 11

如果您的AreaRegistration类和Controller类不在同一名称空间中,您将处于这种情况 - 路由将匹配(并且RouteDebugger将确认此情况)但您将在"正确"的URL处获得404.

解决方案是确保两个clases在同一名称空间中.

  • 还要确保控制器类已正确命名.我花了2个小时调试,因为文件名是正确的,但类名不是 (6认同)

Ste*_*ory 1

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

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
);
Run Code Online (Sandbox Code Playgroud)