18

我想让我的基本 URL 转到在线商店的特定类别(如果有区别的话,就是NopCommerce在线商店)。该类别的网址是: http://myUrl.com/c/6

在阅读了包括 Scott Gutherie 的关于 MVC 路由的帖子在内的一些帖子之后,我想我可以将以下代码添加到我的 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

4 回答 4

14

看起来最有趣的部分在 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 合作过。

于 2011-12-12T06:10:07.897 回答
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" }

于 2011-12-12T06:08:56.363 回答
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;
        }
    }
}
于 2012-10-03T10:23:07.900 回答
0

你有没有尝试过:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
            "Default", // Route name
            "Catalog/Category/6"
    );
}
于 2011-12-12T06:08:28.493 回答