2013-08-07 49 views
1

我想設置爲以下控制器在Global.asax中的路線,但我總是得到HTTP錯誤404.0 - 找不到如何在MVC中爲控制器設置路由。始終返回「HTTP錯誤404.0 - 未找到」

AssetsController.cs

public class AssetsController : Controller 
{ 
    public ActionResult Index(string aDesc, string fDate) 
    { 
    ViewBag.DateFrom = fDate.ToString(); 
    ViewBag.AssetDescription = aDesc.ToString(); 
    return View(); 
    } 
} 

的Global.asax.cs

public class MvcApplication : System.Web.HttpApplication 
{ 

    public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.MapRoute("Assets", "Assets/{action}/{aDesc}/{fDate}", 
      new { controller = "Assets", action = "Index", aDesc = "", fDate = "" } 
     ); 

     // Show a 404 error page for anything else. 
     routes.MapRoute("Error", "{*url}", 
      new { controller = "Error", action = "404" } 
     ); 
    } 

    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     WebApiConfig.Register(GlobalConfiguration.Configuration); 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 
     AuthConfig.RegisterAuth(); 

    } 
} 

當我ACCE SS使用以下URL的觀點,一切工作正常,並預期:

http://localhost/Assets/Index/?aDesc=090&fDate=20130701000000 

但是,當我使用以下網址訪問的觀點,我得到404.0錯誤信息(如下圖所示的錯誤)

http://localhost/Assets/Index/090/20130701000000 

錯誤:HTTP錯誤404.0 - 未找到您正在查找的資源已被刪除,其名稱已更改或暫時不可用。

程序池設置爲.NET 4.0集成模式

HTTP重定向和靜態內容是根據常見HTTP功能檢查(Windows功能開/關)

+0

我不知道這個問題有什麼做的的WebAPI設置和事實的WebAPI的路線正在建立第一個註冊的路線? – JayC

+0

JayC我不得不在RouteConfig.cs中註冊路由來修復它。謝謝。 – Learner

回答

1

我不知道你的路線正在超越默認情況下,因爲您明確提供參數。嘗試在RouteConfig.cs

http://www.jondavis.net/techblog/post/2012/06/23/ASPNET-MVC-4-Where-Have-All-The-Globalasax-Routes-Gone.aspx

+0

謝謝B Cotter。我昨晚解決了這個問題。這正是你在上面指定的。我通過在App_Start> RouteConfig.cs中註冊路由來修復它。將您的答案標記爲正確,而不是回答我自己的問題並接受它。謝謝。 – Learner

相關問題