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

tzxcd3kk  于 2023-01-27  发布在  .NET
关注(0)|答案(5)|浏览(240)

我想让我的基本URL转到在线商店的特定类别(NopCommerce在线商店,如果这样做有区别的话)。该类别的URL为: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几乎没有经验,所以如果其中任何一点没有意义,我道歉。

pbpqsu0x

pbpqsu0x1#

看起来像这最有趣的位是在这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合作过。

eiee3dmh

eiee3dmh2#

尝试将其写入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" }

hjqgdpho

hjqgdpho3#

为了避免未来与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;
        }
    }
}
iyr7buue

iyr7buue4#

您是否尝试过:

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

eiee3dmh5#

对于MVC 6,设置默认的加载主页或控制器操作
1.编辑Program.cs文件
1.您将在Program.cs文件中看到一些代码行

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=home}/{action=Index}/{id?}"
    );

1.现在更改pattern中的值:键,例如,假设要加载的默认控制器\页面来自“abc控制器”、XYZ操作......,然后在上面的示例代码中,将home值替换为“abc”,将Index值替换为“XYZ

相关问题