Nick
Nick

Reputation: 19664

Why am I getting 404 when the route matches? ASP.Net MVC

I have been stuck on this issue for several hours now.

I have a controller called 'DecisionPoint' and I have a breakpoint set on it's 'ApplicationState' action. No matter what I try I keep getting a 404 in the browser. I suspected that my route was not correct so I downloaded a route debugger and it turns our the URLs I am trying match the Controller and the action. So why do I get a 404 and never see the breakpoint hit?

/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");

Route

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

I create a whole new controller and it works. This is now what my route table looks like. The state controller correclty routes to 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);

    }
}

So I am stumped..

Upvotes: 13

Views: 21803

Answers (9)

SirPeace
SirPeace

Reputation: 109

Also note that controller class must have public access modifier. I wrote down my controller as:

[ApiController]
[Route("[controller]")]
class HomeController : ControllerBase
{
    ...
}

And since default modifier for classes is internal, all actions within this controller didn't match any route.

Upvotes: 0

Enrico
Enrico

Reputation: 3443

I was missing

app.MapControllers();

in my program.cs (.NET 6 and above)

Upvotes: 3

JacobIRR
JacobIRR

Reputation: 8946

Dumb, but it got me: Be sure your controller inherits from Controller

Upvotes: 3

jaminto
jaminto

Reputation: 3955

If your AreaRegistration class and Controller class are not in the same namespace, you'll be in this situation - the route will match (and RouteDebugger will confirm this) but you'll get a 404 at the "correct" url.

The solution is to make sure both clases are in the same namespace.

Upvotes: 11

starlocke
starlocke

Reputation: 3661

In my case, the answer for the same problem was a matter of needing to "include in project" the relevant controllers and views instead of incorrect routing rules.

When mine were created, they weren't automatically included for some reason. This problem was revealed after I closed and re-opened the solution.

{+1 hate} awarded to Visual Studio for its faulty hyper-automation sending me digging through Web.Config files, trying to tack on extensions, and even trying (and failing) to whip up a decent ErrorController.

Upvotes: 1

Ariel De Prada
Ariel De Prada

Reputation: 161

I just recently ran into this same problem. For me the problem was that I had two controllers with the same name if different namespaces (One was in an area).

Upvotes: 2

Connell
Connell

Reputation: 14411

This can happen due to your Controller being in another project which references a different version of System.Web.Mvc!

I had the same symptoms - the correct route was found, but got a 404! On HttpHandler.ProcessRequest, an exception was thrown saying the handler's controller does not implement IController.

Upvotes: 2

Trent Scholl
Trent Scholl

Reputation: 2387

Make sure your controller class is called DecisionPointController and not DecisionPoint

Upvotes: 12

Steve Mallory
Steve Mallory

Reputation: 4283

I'm no routes guru, but I have always added all parameters for the default argument:

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
);

Upvotes: 1

Related Questions