我正在通過Pro Asp.net mvc3框架書。我想要更改默認路由,以便我可以擁有不同的主頁。我添加了一個名爲Pages的新控制器和一個名爲Home的視圖。這就是我想要的主頁。mvc如何更改默認路由
我試着加入這個對我的global.asax.cs
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
new { controller = "Pages", action = "Home", id = "DefautId" });
這改變了默認頁面,但它搞砸了的類別
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(null,
"", // Only matches the empty URL (i.e. /)
new
{
controller = "Product",
action = "List",
category = (string) null,
page = 1
}
);
routes.MapRoute(null,
"Page{page}", // Matches /Page2, /Page123, but not /PageXYZ
new {controller = "Product", action = "List", category = (string) null},
new {page = @"\d+"} // Constraints: page must be numerical
);
routes.MapRoute(null,
"{category}", // Matches /Football or /AnythingWithNoSlash
new {controller = "Product", action = "List", page = 1}
);
routes.MapRoute(null,
"{category}/Page{page}", // Matches /Football/Page567
new {controller = "Product", action = "List"}, // Defaults
new {page = @"\d+"} // Constraints: page must be numerical
);
routes.MapRoute(null, "{controller}/{action}");
}
我應該怎麼做,使這個工作?
UPDATE:
URLS: 主頁進入列表項的
http://localhost/SportsStore/
點擊類別
http://localhost/SportsStore/Chess?contoller=Product
控制器,被擊中在主頁
public class ProductController : Controller
{
private readonly IProductRepository repository;
public int PageSize = 4;
public ProductController(IProductRepository repoParam)
{
repository = repoParam;
}
public ViewResult List(string category, int page = 1)
{
var viewModel = new ProductsListViewModel
{
Products = repository.Products
.Where(p => category == null || p.Category == category)
.OrderBy(p => p.ProductID)
.Skip((page - 1)*PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = category == null
? repository.Products.Count()
: repository.Products.Where(
e => e.Category == category).Count()
},
CurrentCategory = category
};
return View(viewModel);
}
控制器,我想被擊中的主頁
public class PagesController : Controller
{
public ViewResult Home()
{
return View();
}
}
感謝,
+1你可以給這個Route路由的Url例子嗎? .MapRoute(null,「」,new {controller =「Pages」,action =「Home」});' – 2013-07-26 14:32:01