2013-03-05 78 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