How to set default page in MVC application?

I would like my base URL to go into a specific category of online store ( NopCommerce online store, if that matters). Category URL: http://myUrl.com/c/6

After reading a few posts, including Scott Gutherie 's MVC routing post, I thought I could just add the following code to the Global.ascx.cs file:

  public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //register custom routes (plugins, etc) var routePublisher = EngineContext.Current.Resolve<IRoutePublisher>(); routePublisher.RegisterRoutes(routes); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Catalog", action = "Category", id = 6 }, new[] { "Nop.Web.Controllers" } ); } 

But that didn't seem to work. How can I accomplish what I'm trying to do?

I have little experience with MVC, so I apologize if that doesn't make sense.

+16
asp.net-mvc nopcommerce
Dec 12 2018-11-12T00:
source share
4 answers

looks like the most interesting bits in the nopcommerce source code. the default route is registered as

  routes.MapLocalizedRoute("HomePage", "", new { controller = "Home", action = "Index"}, new[] { "Nop.Web.Controllers" }); 

basically you want to register your default route first, before the comment //register custom routes . should look like this:

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Catalog", action = "Category", id = 6 }, new[] { "Nop.Web.Controllers" } ); routes.MapRoute( "CustomHome", // Route name "", // URL with parameters new { controller = "Catalog", action = "Category", id = 6 }, new[] { "Nop.Web.Controllers" } ); //register custom routes (plugins, etc) var routePublisher = EngineContext.Current.Resolve<IRoutePublisher>(); routePublisher.RegisterRoutes(routes); } 

the first route may not even be needed. I'm not sure. never worked with nopcommerce.

+13
Dec 12 '11 at 6:10
source share

To avoid any conflicts with updates in NopCommerce, I would like to create a new RouteProvider.cs inside my theme folder, like this:

 ~/Themes/MyTheme/Infrastructure/RouteProvider.cs 

Then enter this code inside:

 namespace Nop.Web.Themes.MyTheme.Infrastructure { public class RouteProvider : IRouteProvider { public void RegisterRoutes(RouteCollection routes) { routes.MapLocalizedRoute("CustomHome", "", new { controller = "Catalog", action = "Category", Id = 6 }, new[] { "Nop.Web.Controllers" }); } public int Priority { get { return 10; } } } 
+1
03 Oct. '12 at 10:23
source share

You tried:

 public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute( "Default", // Route name "Catalog/Category/6" ); } 
0
Dec 12 2018-11-12T00:
source share

Try just writing this in the RegisterRoutes method

  public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Catalog", action = "Category", id = 6 } // Parameter defaults ); } 

it should set your default page from / Catalog / Category / 6

I don’t understand why you are writing this line new[] { "Nop.Web.Controllers" }

0
Dec 12 2018-11-12T00:
source share



All Articles