Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a default page on an MVC app?

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.

like image 796
Abe Miessler Avatar asked Dec 12 '11 05:12

Abe Miessler


2 Answers

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" }

like image 23
Artur Keyan Avatar answered Oct 11 '22 21:10

Artur Keyan


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.

like image 172
nathan gonzalez Avatar answered Oct 11 '22 23:10

nathan gonzalez