MVC如何更改默认路由 [英] mvc how to change default route
本文介绍了MVC如何更改默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我经历临Asp.net MVC3框架的书。我想改变默认路由,这样我可以有不同的主页。我添加了一个名为新的页面控制器和一个叫主视图。这是我想什么是我的主页。
I'm going through the Pro Asp.net mvc3 framework book. I wanting to change the default route so that I can have a different home page. I've added a new controller called Pages and a view called Home. This is what I'm wanting as my home page.
我试着将其加入我的global.asax.cs
I've tried adding this to my global.asax.cs
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
new { controller = "Pages", action = "Home", id = "DefautId" });
这改变默认的页面,但它搞砸了的类别
This changes the default page, but it screws up the categories
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}");
}
我应该怎么做,使这项工作?
What should I do to make this work?
更新:
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();
}
}
感谢,
推荐答案
我能得到像这样的工作:
I was able to get it to work like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(null, "", new {controller = "Pages", action = "Home"});
//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}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {controller = "Pages", action = "Home", id = UrlParameter.Optional} // Parameter defaults
);
//routes.MapRoute("MyRoute", "{controller}/{action}",
// new { controller = "Pages", action = "Home" });
有没有更好的办法?
is there a better way?
这篇关于MVC如何更改默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文