Evgeny
Evgeny

Reputation: 3322

ASP.NET MVC 4: One controller routing is not like the others

I have added a new controller, "LoggingController" to an MVC 4 application that already has several controllers. Now I noticed that the routing behaviour for this controller is different from those that already existed.

For example, the following two urls work properly, as expected, and hit the "Index" method in BlogController.

http://localhost:56933/Blog/

http://localhost:56933/Blog/Index

So do all other controllers except the one I added:

http://localhost:56933/Logging/Index - works properly, hits the "Index" method in LoggingController

http://localhost:56933/Logging/ - returns "Server Error in '/' Application. The resource cannot be found."

Where should I start looking, beyond the obvious things?

Here is the Index signature from LoggingController

public ActionResult Index(int? page, string Period, string LoggerProviderName, string LogLevel, int? PageSize)

There is nothing specific to a certain controller in the MapRoute. Here is the full RouteConfig for reference:

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

    routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

    routes.MapRoute(
    name: "Display",
    url: "Post/{id}/{seofriendly}",
    defaults: new { controller = "Post", action = "Display", id = UrlParameter.Optional, seofriendly = ""}
        );

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

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

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

Upvotes: 0

Views: 531

Answers (1)

Michael Dunlap
Michael Dunlap

Reputation: 4310

Your Index method doesn't actually have a signature matched by any of your available routes. You'll have to either provide defaults to your Index action or add an additional route to the beginning of your route table to specify those defaults. For Example:

routes.MapRoute(
    name: "LoggingDefaults",
    url: "Logging/{Period}/{LoggerProviderName}/{LogLevel}/{page}/{PageSize}",
    defaults: new {controller = "Logging", 
                   action = "Index",
                   page = UrlParameter.Optional,
                   PageSize = UrlParameter.Optional,
                   LoggerProviderName = "SomeProvider",
                   Period = "SomePeriod",
                   LogLevel = "SomeLevel"}

Upvotes: 2

Related Questions