如何在MVC应用程序上设置默认页面?

时间:2011-12-12 05:51:19

标签: asp.net asp.net-mvc nopcommerce

我想让我的基本网址转到在线商店的特定类别(NopCommerce在线商店,如果这有所不同)。该类别的网址为:http://myUrl.com/c/6

在阅读了包括Scott Gutherie的帖子about MVC routing在内的一些帖子后,我想我可以将以下代码添加到我的Global.ascx.cs文件中:

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

但这似乎不起作用。我怎样才能完成我想做的事情?

我对MVC的经验不多,所以如果其中任何一个没有意义,我会道歉。

4 个答案:

答案 0 :(得分:13)

看起来像nopcommerce源代码中最有趣的位。默认路由注册为

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

你基本上想要在//register custom routes评论之前注册你的默认路线。应该看起来像这样:

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


}

甚至可能不需要第一条路线。我不确定。从未与nopcommerce合作过。

答案 1 :(得分:1)

尝试在 RegisterRoutes 方法

中编写此代码
    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
        );

    }

必须从/ Catalog / Category / 6

设置默认页面

我不明白为什么要写这一行new[] { "Nop.Web.Controllers" }

答案 2 :(得分:1)

为了避免将来与NopCommerce中的更新发生冲突,我要做的是在我的主题文件夹中创建一个新的RouteProvider.cs:

~/Themes/MyTheme/Infrastructure/RouteProvider.cs

然后将此代码放入:

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

答案 3 :(得分:0)

你试过了吗?

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
            "Default", // Route name
            "Catalog/Category/6"
    );
}