嗨,我正在尝试为我的 URL 定义路由路径。这是目前的样子:
routes.MapRoute(
name: "Category",
url: "{controller}/DisplayBooksByCategory/{categoryId}/{pageNumber}",
defaults: new { controller = "Home", action = "DisplayBooksByCategory" }
);
这可行,但它不完全是我想要的。目前我的网址看起来像这样:
http://localhost:51208/Home/DisplayBooksByCategory/7/1
我想要的是能够将 categoryId 替换为实际的类别名称。类别名称不会作为参数传递给操作,只有 categoryId。这是我的操作:
public ActionResult DisplayBooksByCategory(int pageNumber, int categoryId)
{
int bookByCategoryCount = bookRepository.CountBooksByCategory(categoryId);
var entities = new BooksAndCategories
{
Books = bookRepository.GetBookByCategory(categoryId, pageNumber, numberOfBooksOnPage),
PageNumber = pageNumber,
LastPageNumber = (bookByCategoryCount / numberOfBooksOnPage) + 1,
NumberOfBooksPerPage = numberOfBooksOnPage,
Categories = categoryRepository.GetCategories(),
CurentPage = "ProductManager",
CurentCategory = categoryId
};
return View("DisplayBooksByCategory", entities);
}
有没有办法使用路由 API 来做到这一点而无需修改操作?