hackp0int
hackp0int

Reputation: 4161

MVC3 Areas routing conflict

Question: i want my route to be like that

/admin/main/category/1 -> 1 == ?page=1 i don't want page=1 to be seen

My Controller

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:

The parameters dictionary contains a null entry for parameter 'page' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Category(Int32)' in 'WebUI.Areas.Admin.Controllers.MainController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

Thank you all in advance.

Upvotes: 0

Views: 495

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039080

Make sure that in your Admin area route registration you have defined the {page} route token instead of {id} which is generated by default:

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

Now when you are generating links make sure you specify this parameter:

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

will emit:

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

and when this url is requested the Category action will be invoked and passed page=5 parameter.

Upvotes: 1

Related Questions