URL rewrite in MVC3

本文关键字:MVC3 in rewrite URL | 更新日期: 2023-09-27 17:52:55

我有一个页面,它通过GET或post传递其id来显示一个帖子。

例如

:www.example.com/post/viewpost ? id = PostTiTle

但是我想让实现这样: www.example.com/post/PostTiTle

所以post id应该在URL中使用单个控制器。我怎样才能做到这一点呢?

URL rewrite in MVC3

如果你没有修改任何默认的MVC路由示例应该工作。

例子
public ActionResult Index(string id)
{
    // do something with variable id = PostTiTle
}

如果你的控制器名称是Post,而你的动作名称是ViewPost,那么你需要像这样添加额外的路由。

routes.MapRoute(
    "Post",
    "post/{id}",
    new { controller = "Post", action = "ViewPost", id = UrlParameter.Optional }
);