如何在MVC3中重写URL

时间:2012-09-11 05:48:21

标签: asp.net-mvc-3 url url-routing

我有这个网址http://example.com/Book/Details?Bookid=15这个网址

我想像这样http://example.com/Book/Details/BlackBook/

重写网址

请帮助谢谢

1 个答案:

答案 0 :(得分:4)

将以下路由添加到global.asax.cs RegisterRoutes:

routes.MapRoute(
            "Book By Name",
            "Book/Details/{bookName}",
             new { controller = "Book", action = "BookDetails" }
        );

你需要一个BookController,其中包含以下Action方法:

public Book BookDetails(string bookName)
{
    // Your logic here to get a book by name and return it
}

或者,您可以使用默认路线并在Details(string id)内创建BookController操作方法以获得相同的结果