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

18

我希望我的基础URL可以指向在线商店的特定类别(如果这是NopCommerce在线商店,则不同的),该类别的URL为: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的经验很少,所以如果有什么不合理的地方,请谅解。


放置了那段代码会发生什么?看当前路由,你必须有更多的路由规则,因为它不遵循那里所规定的命名结构。你能否发布整个注册路由部分? - nathan gonzalez
已更新完整的RegisterRoutes方法。当我访问基本URL时,它跳转到之前相同的页面。 - Abe Miessler
5个回答

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。


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


1
他正在现有的电子商务框架上进行开发。他不能仅仅删除代码。幕后有很多路由注册工作。这基本上会导致他的应用程序停止工作... - nathan gonzalez

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

0

对于MVC 6,设置默认加载主页或控制器操作

  1. 编辑Program.cs文件
  2. 您将在Program.cs文件中看到一些代码行
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=home}/{action=Index}/{id?}"
    );
  1. 现在更改模式内的值:key,例如假设您要加载的默认控制器\页面来自“abccontroller”,XYZ操作....然后在上面的示例代码中将home值替换为“abc”,将Index值替换为“XYZ”

0

你试过了吗:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接