Note that there are some explanatory texts on larger screens.

plurals
  1. POmvc how to change default route
    primarykey
    data
    text
    <p>I'm going through the Pro Asp.net mvc3 framework book. I wanting to change the default route so that I can have a different home page. I've added a new controller called Pages and a view called Home. This is what I'm wanting as my home page.</p> <p>I've tried adding this to my global.asax.cs</p> <pre><code>routes.MapRoute("MyRoute", "{controller}/{action}/{id}", new { controller = "Pages", action = "Home", id = "DefautId" }); </code></pre> <p>This changes the default page, but it screws up the categories </p> <pre><code> public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(null, "", // Only matches the empty URL (i.e. /) new { controller = "Product", action = "List", category = (string) null, page = 1 } ); routes.MapRoute(null, "Page{page}", // Matches /Page2, /Page123, but not /PageXYZ new {controller = "Product", action = "List", category = (string) null}, new {page = @"\d+"} // Constraints: page must be numerical ); routes.MapRoute(null, "{category}", // Matches /Football or /AnythingWithNoSlash new {controller = "Product", action = "List", page = 1} ); routes.MapRoute(null, "{category}/Page{page}", // Matches /Football/Page567 new {controller = "Product", action = "List"}, // Defaults new {page = @"\d+"} // Constraints: page must be numerical ); routes.MapRoute(null, "{controller}/{action}"); } </code></pre> <p>What should I do to make this work? </p> <p><strong>UPDATE:</strong></p> <p>URLS: <em>Home page goes to List of items</em></p> <pre><code>http://localhost/SportsStore/ </code></pre> <p><em>clicked category</em> </p> <pre><code>http://localhost/SportsStore/Chess?contoller=Product </code></pre> <p><strong>Controller that is hit for the home page</strong></p> <pre><code> public class ProductController : Controller { private readonly IProductRepository repository; public int PageSize = 4; public ProductController(IProductRepository repoParam) { repository = repoParam; } public ViewResult List(string category, int page = 1) { var viewModel = new ProductsListViewModel { Products = repository.Products .Where(p =&gt; category == null || p.Category == category) .OrderBy(p =&gt; p.ProductID) .Skip((page - 1)*PageSize) .Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = category == null ? repository.Products.Count() : repository.Products.Where( e =&gt; e.Category == category).Count() }, CurrentCategory = category }; return View(viewModel); } </code></pre> <p><strong>Controller that I'm wanting to be hit for the home page</strong></p> <pre><code>public class PagesController : Controller { public ViewResult Home() { return View(); } } </code></pre> <p>thanks,</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload