0

Improving urls

Currently I have links in the form (displaying product information):

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

I want to clean this up into the form:

http://localhost:XXXX/Products/Page1

I think I need to do this with routes.MapRoute, something like so:

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

This was put above the default route (so should override I am led to believe)

The product controller looks like this:

    //
    // 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);
    }

What am I doing wrong?

1 Answer 1

2

Change routes.MapRoute

routes.MapRoute(null, "Products/Page{page}", new {controller = "Products", action = "Index"});
Sign up to request clarification or add additional context in comments.

1 Comment

Correct - the Convention is that all controller classes END with the word Controller. As such, the routes then don't need you to add that full word into the route details, as above. So like @Akyegane says, controller = "Products" .. not "ProductsController"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.