改进asp.net mvc2中的URL

时间:2010-08-30 04:34:44

标签: c# asp.net-mvc visual-studio-2010 asp.net-mvc-2

改善网址

目前我在表单中有链接(显示产品信息):

http://localhost:XXXX/Products/?page=1

我想将其清理成以下形式:

http://localhost:XXXX/Products/Page1

我认为我需要使用routes.MapRoute执行此操作,如下所示:

routes.MapRoute(null, "/Products/Page{page}", new {controller = "ProductController", action = "Index"});

这被置于默认路线之上(所以应该覆盖我导致相信)

产品控制器如下所示:

    //
    // GET: /Products/
    public ActionResult Index([DefaultValue(1)] int page)
    {
        var productsToShow = //omitted for simplicity

        var viewModel = new ProductIndexViewModel
                            {
                                ProductList = //omitted for simplicity,
                                PagingInfo = new PagingInfo
                                                 {
                                                     CurrentPage = page,
                                                     ItemsPerPage = PageSize,
                                                     TotalItems = productsToShow.Count()
                                                 }
                            };

        //Passed to view as ViewData.Model (or simply Model)
        return View(viewModel);
    }

我做错了什么?

1 个答案:

答案 0 :(得分:2)

更改routes.MapRoute

routes.MapRoute(null, "Products/Page{page}", new {controller = "Products", action = "Index"});