18

I would like to have my base URL go to a specific category of an online store (a NopCommerce online store if that makes a difference). The URL of the category is: http://myUrl.com/c/6

After reading a few posts including Scott Gutherie's post about MVC routing I thought I could just add the following code to my 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 this didn't seem to work. How can I accomplish what I am trying to do?

I have little experience with MVC so I apologize if any of this does not make sense.

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
  • what happens with that code in place? looking at the current route, you must have more routing rules in place than that one, as it doesn't follow the naming structure laid out there. any chance you could post the whole register routes section? – nathan gonzalez Dec 12 '11 at 05:56
  • Updated with the full RegisterRoutes method. When I go to the base URL it goes to the same page that it did before. – Abe Miessler Dec 12 '11 at 06:05

5 Answers5

14

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

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

you'll basically want to register your default route first, before the //register custom routes comment. should end up looking 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 necessary. i'm not sure. never worked with nopcommerce.

nathan gonzalez
  • 11,817
  • 4
  • 41
  • 57
1

In order to avoid any future conflicts with updates in NopCommerce, what I would do is to create a new RouteProvider.cs inside my theme folder as this:

~/Themes/MyTheme/Infrastructure/RouteProvider.cs

Then put 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;
        }
    }
}
Carlos Martinez T
  • 6,458
  • 1
  • 34
  • 40
1

Try just write this in 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 is must set your default page from /Catalog/Category/6

I don't understand why you write this line new[] { "Nop.Web.Controllers" }

Artur Keyan
  • 7,643
  • 12
  • 52
  • 65
  • 1
    he's building on top of a existing ecommerce framework. he can't just remove the code. there is a lot of route registration that goes on behind the scenes. this would basically cause his application to quite working... – nathan gonzalez Dec 12 '11 at 06:16
0

For MVC 6 , to set the default load home page or contoller action

  1. Edit Program.cs file
  2. You shall see some line of code inside the Program.cs file
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=home}/{action=Index}/{id?}"
    );
  1. Now change the value inside pattern: key , example let's say your default controller\page to load is from "abccontroller" , XYZ action ....then replace the home value with "abc" and Index value with "XYZ" on above sample code
HO LI Pin
  • 1,441
  • 13
  • 13
0

have you tried:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
            "Default", // Route name
            "Catalog/Category/6"
    );
}
AceMark
  • 701
  • 1
  • 11
  • 21