asp.net - 为什么当路由匹配时我会收到 404? ASP.Net MVC

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

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

我有一个名为“DecisionPoint”的 Controller ,并且在其“ApplicationState”操作上设置了一个断点。无论我如何尝试,我都会在浏览器中收到 404 错误。我怀疑我的路线不正确,所以我下载了一个路线调试器,它会将我尝试的 URL 与 Controller 和操作相匹配。那么为什么我会得到 404 并且从未看到断点命中?

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

Controller :

 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**

我创建了一个全新的 Controller 并且它可以工作。这就是我的路由表现在的样子。状态 Controller 正确路由到 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);

    }
}

所以我很困惑..

最佳答案

确保您的 Controller 类名为 DecisionPointController 而不是 DecisionPoint

关于asp.net - 为什么当路由匹配时我会收到 404? ASP.Net MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6012481/

相关文章:

c# - 如果我使用 OWIN Startup.cs 类并将所有配置移到那里,我是否需要 Global.asax.cs 文件?

ASP.Net MVC 使用路由处理段

asp.net - 将WebMatrix项目迁移到VS2010

javascript - 来自其他站点的链接应打开该站点的第一个链接

c# - db.SaveChanges() 期间出现 "Invalid Object Name"错误( Entity Framework /MVC)

c# - MVC4 API 路由进入第一个 Action

asp.net-mvc - 我的 bin 部署的 MVC4 应用程序中的区域路由有什么问题?

c# - 我需要为 ASP.NET 网站创建项目文件

asp.net - "Unable To Connect To Any Of The Specified MySQL Hosts"

c# - 'ValueTuple<T1, T2 >' exists in both ' System.ValueTuple .. .' and ' mscorlib ...'