我正在阅读Pro Asp.net mvc3框架书。我想更改默认路由,以便我可以拥有不同的主页。我添加了一个名为Pages的新控制器和一个名为Home的视图。这就是我想要的主页。
我已经尝试将此添加到我的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}");
}
我该怎么做才能使这项工作成功?
更新
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();
}
}
感谢,
答案 0 :(得分:4)
我能够让它像这样工作:
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" });
有更好的方法吗?
答案 1 :(得分:1)
第一种选择是使用这种页面路由方式:
routes.MapRoute("Give route a name btw","Page/{page}", // Matches /Page/2
new {controller = "Product", action = "List", category = urlParameter.Optional},
new {page = @"\d+"}
);
这样你的路线将更加REST。
其他方式 - 使用正则表达式路由Defining routes using regular expressions
PS:现在无法检查此链接是否在线。几天前还不错。
PS2:Faust对路线顺序是正确的。贪婪的路线是最后的。
PS3:你能写出你想要实现的URL方案吗?
答案 2 :(得分:0)
确保将默认路由放在路由映射的最后。如果它是最后一次,它就无法搞砸类别路线。
<强>更新强> 如果这条路线:
routes.MapRoute(null, "{controller}/{action}");
出现在默认值之前,除了具有第三个URL参数(id)的项目之外,它将捕获您希望您的默认路由捕获的任何内容。
例如:
/somepage/home
会被上面的路线捕获,而不是你的默认路线。
所以你可能想要删除这条路线。