2013-03-05 83 views
0

我已经添加了一个新的控制器,“LoggingController”到已经有几个控制器的MVC 4应用程序。 现在我注意到,该控制器的路由行为与已经存在的路由行为不同。ASP.NET MVC 4:一个控制器路由不像其他

例如,以下两个url正常工作,并按下BlogController中的“Index”方法。

http://localhost:56933/Blog/ 

http://localhost:56933/Blog/Index 

那么做,除非一个我添加的所有其他控制器:

http://localhost:56933/Logging/Index - 工作正常,打在LoggingController

http://localhost:56933/Logging/ “索引” 的方法 - 返回“服务器错误 '/' 应用。 没有找到您要查的资源。”

我应该从哪里开始寻找,超越了显而易见的事物?

这里是LoggingController

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

指数的签名没有什么具体在图路线一定的控制。以下是完整的RouteConfig供参考:

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

回答

2

您的索引方法实际上没有任何可用路由匹配的签名。您必须为您的索引操作提供默认值,或者在路由表的开头添加其他路由以指定这些默认值。例如:

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"} 
+0

我明白了!我选择了默认值。奇怪的是,当我刚刚修改索引方法时,这还不够。当我放弃并重新创建控制器+索引时,它开始按预期工作。无论如何,现在都很好。 – Evgeny 2013-03-05 23:50:29