我在控制器中有这段代码,它接受一个类作为参数
public ActionResult Index(PaginationModel paginationViewModel = null)
{
//do some logic here
return View(viewModel);
}
这就是模型的样子
public class PaginationModel
{
protected const int DisplayPageRange = 2;
//Pagination Related Properties
public int CurrentPage { get; set; }
public int TotalPages { get; set; }
public int TotalItems { get; set; }
public string PageName { get; set; }
public int ItemsPerPage { get; set; }
public int DefaultItemsPerPage { get; set; }
public string PagingText { get; set; }
//Filters
public string Search { get; set; }
public string Category { get; set; }
public string Star { get; set; }
public string Director { get; set; }
public string Writer { get; set; }
}
我在视图中调用动作
<a href="/?Category=@genre">@genre</a>
一切似乎都很好,但我想格式化 URL,所以它看起来像
Movie/Index/Horror/1 //Horror is the category and 1 is the current page number
目前,它正在显示
Movie/Index?Category=Horror&Page=1
我可以使用单独的参数而不是在动作中使用类来做到这一点
public ActionResult Index(string category, int pagenum, etc etc)
{
}
但是有没有办法在 RouteConfig 中使用类作为参数来做到这一点?
编辑:
我的 RouteConfig 是
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "MovieSearch",
url: "Movie/Index/{Category}",
defaults: new { controller = "Movie", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdminSearch",
url: "{controller}/{action}/{key}",
defaults: new {controller = "Admin", action = "Search", id = UrlParameter.Optional}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Movie", action = "Index", id = UrlParameter.Optional }
);
}