0

问题:我希望我的路线是这样的

/admin/main/category/1 -> 1 == ?page=1 我不想看到 page=1

我的控制器

public class MainController : BaseController
{
    private const int PageSize = 5; //pager view size

    [Inject]
    public ICategoryRepository CategoryRepository { get; set; }

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Category(int page)
    {
        //int pageIndex = page.HasValue ? page.Value : 1;
        int pageIndex = page != 0 ? page : 1; 
        return View("Category", CategoryViewModelFactory(pageIndex));
    }

    /*
     *Helper: private instance/static methods
     ======================================================================*/
    private CategoryViewModel CategoryViewModelFactory(int pageIndex) //generate viewmodel category result on pager request
    {
        return new CategoryViewModel
        {
            Categories = CategoryRepository.GetActiveCategoriesListDescending().ToPagedList(pageIndex, PageSize)
        };
    }
}



  public class AdminAreaRegistration : AreaRegistration
  {
        public override string AreaName
        {
            get
            {
                return "admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRouteLowercase(
                "AdminCategoryListView", 
                "admin/{controller}/{action}/{page}",
                new { controller = "Category", action = "Category", page = "1" },
                new { id = @"\d+" },
                new[] { "WebUI.Areas.Admin.Controllers" }
            );
        }
    }

My Exception:

参数字典包含“WebUI.Areas.Admin.Controllers.MainController”中方法“System.Web.Mvc.ActionResult Category(Int32)”的不可为空类型“System.Int32”的参数“页面”的空条目。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数

谢谢大家。

4

1 回答 1

1

确保在您的管理区域路由注册中定义了{page}路由令牌,而不是{id}默认生成的路由令牌:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{page}",
        new { action = "Index", page = UrlParameter.Optional }
    );
}

现在,当您生成链接时,请确保指定此参数:

@Html.ActionLink(
    "go to page 5",                         // linkText
    "category",                             // actionName
    "main",                                 // controllerName
    new { area = "admin", page = "5" },     // routeValues
    null                                    // htmlAttributes
)

将发出:

<a href="/Admin/main/category/5">go to page 5</a>

当请求此 url 时,将调用 Category 操作并传递page=5参数。

于 2012-05-11T09:41:19.060 回答