2015-12-21 66 views
0

在我的RouteConfig中添加了產品路由後,我的默認主頁更改爲產品頁面。我如何將我的家庭控制器重新設置爲我的默認主頁。ASP.NET MVC 4不正確的首頁路線

 routes.MapRoute(
      name: "Product", 
      url: "{controller}/{action}", 
      defaults: new { controller = "Product", action = "Index" } 
     ); 


     routes.MapRoute(
      name: "Products", 
      url: "products/{categoryName}/{Id}", 
      defaults: new { controller = "Products", action = "Index", categoryName = "", Id = UrlParameter.Optional } 
     ); 


     routes.MapRoute(
      name: "Default", 
      url: "{action}", 
      defaults: new { controller = "Home", action = "Index"} 
     ); 
+0

試試菲爾哈克的路線測試工具。 [這篇文章](http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx/)描述它,這裏是[NuGet包](https://www.nuget.org /包/ routedebugger /)。 – ourmandave

回答

0

我覺得第一個和最後將採取同樣way.The程序會選擇它相匹配,所以你應該硬編碼這樣的第一路線的第一路線。

routes.MapRoute(
     name: "Product", 
     url: "Product/{action}", 
     defaults: new { controller = "Product", action = "Index" } 
    ); 

或者您應該刪除第一條路線。我認爲你應該採用這種方法。

 routes.MapRoute(
      name: "Products", 
      url: "products/{categoryName}/{Id}", 
      defaults: new { controller = "Products", action = "Index", categoryName = "", Id = UrlParameter.Optional } 
     ); 


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

說明:從斯蒂芬·沃爾特的post

默認路由表 包含一個路由(名爲Default)。默認路由將URL的第一個段映射到控制器名稱,將URL的第二個段映射到控制器操作,將第三個段映射到名爲id的參數。

試想一下,你輸入以下網址到瀏覽器的地址欄:

/Home/Index/3 

的默認路由這個URL映射到以下參數:

controller = Home 

    action = Index 

    id = 3 

當你請求的URL /主頁/ Index/3,執行以下代碼:

HomeController.Index(3) 

Default route includ es默認爲所有三個參數。如果您不提供控制器,則控制器參數默認爲值Home。如果您不提供操作,則操作參數默認爲值Index。最後,如果你不提供一個id,id參數默認爲一個空字符串。

讓我們來看幾個默認路由如何將URL映射到控制器操作的例子。試想一下,你輸入以下網址到瀏覽器地址欄:

/Home 

由於缺省路由參數的默認值,進入此網址會導致清單2中HomeController類的Index()方法被調用。

清單2 - HomeController.cs

using System.Web.Mvc; 

namespace MvcApplication1.Controllers 
{ 
    [HandleError] 
    public class HomeController : Controller 
    { 
     public ActionResult Index(string id) 
     { 
      return View(); 
     } 
    } 

}

在清單2,HomeController類包括名爲Index(),它接受名爲Id單個參數的方法。 URL/Home將使用空字符串作爲Id參數的值調用Index()方法。

由於的MVC框架調用控制器動作的方式,URL/Home也符合HomeController類的Index()方法清單3.

清單3中 - HomeController.cs(索引操作沒有參數)

using System.Web.Mvc; 

namespace MvcApplication1.Controllers 
{ 
    [HandleError] 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      return View(); 
     } 
    } 
} 

清單3中的Index()方法不接受任何參數。 URL/Home將導致調用此Index()方法。 URL/Home/Index/3也調用這個方法(Id被忽略)。

的URL/Home也匹配HomeController類的Index()方法清單4.

清單4 - HomeController.cs(可空參數索引動作)

using System.Web.Mvc; 

    namespace MvcApplication1.Controllers 
    { 
     [HandleError] 
     public class HomeController : Controller 
     { 
      public ActionResult Index(int? id) 
      { 
       return View(); 
      } 
     } 
    } 

在清單4 ,Index()方法有一個Integer參數。因爲該參數是一個可爲空的參數(可以具有值Null),所以可以調用Index()而不會引發錯誤。

最後,使用URL/Home調用清單5中的Index()方法會導致異常,因爲Id參數不是可爲空的參數。如果你試圖調用Index()方法,然後你在圖中顯示的錯誤1.

上市5 - HomeController.cs(取值參數指標的行動)

using System.Web.Mvc; 

namespace MvcApplication1.Controllers 
{ 
    [HandleError] 
    public class HomeController : Controller 
    { 
     public ActionResult Index(int id) 
     { 
      return View(); 
     } 
    } 
}