【发布时间】:2012-03-06 21:01:34
【问题描述】:
我是 MVC3 的新手,无法解决这个问题。 我正在制作一个带有帖子的简单博客,这些帖子分为几类,每个帖子都可能有一些标签。 如果我向用户显示帖子,我会在那里进行分页,其中 url 类似于 localhost/Posts/1,其中“1”是页面编号。 但是,如果我只想显示某个类别或带有某个标签的帖子,我该怎么做呢? 它的格式为 localhost/Posts/Categories/1,其中“1”是类别的 ID,或者 localhost/Posts/Tags/tag1,其中“tag1”是特定标签 我想将其全部更改为 localhost/Posts/Page/1 或 localhost/Posts/Categories/1/Page/1 或 localhost/Posts/Tags/tag1/Page/1 格式,但我真的不知道如何在控制器中实现这一点。 所以我的问题是:如何让控制器中的方法接受这些复杂的 url?
我猜它与路由有关,但找不到任何解释我的问题。
非常感谢您的帮助。
我的代码:
public ActionResult Tags(string id)
{
Tag tag = GetTag(id);
ViewBag.IdUser = IDUser;
if (IDUser != -1)
{
ViewBag.IsAdmin = IsAdmin;
ViewBag.UserName = model.Users.Where(x => x.IDUser == IDUser).First().Name;
}
return View("Index", tag.Posts.OrderByDescending(x => x.DateTime));
}
public ActionResult Index(int? id)
{
int pageNumber = id ?? 0;
IEnumerable<Post> posts =
(from post in model.Posts
where post.DateTime < DateTime.Now
orderby post.DateTime descending
select post).Skip(pageNumber * PostsPerPage).Take(PostsPerPage + 1);
ViewBag.IsPreviousLinkVisible = pageNumber > 0;
ViewBag.IsNextLinkVisible = posts.Count() > PostsPerPage;
ViewBag.PageNumber = pageNumber;
ViewBag.IdUser = IDUser;
if (IDUser != -1)
{
ViewBag.IsAdmin = IsAdmin;
ViewBag.UserName = model.Users.Where(x => x.IDUser == IDUser).First().Name;
}
return View(posts.Take(PostsPerPage));
}
【问题讨论】:
标签: c# asp.net-mvc-3 url routing