如何在一个MVC应用程序中设置默认页? [英] How to set a default page on an MVC app?

查看:119
本文介绍了如何在一个MVC应用程序中设置默认页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有我的基础URL去在线商店的一个特定类别(一 NopCommerce 的在线商店,如果有差别)。该类别的网址是: http://myUrl.com/c/6

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

阅读几个职位,包括斯科特Gutherie的帖子<经过href=\"http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx\">about MVC路由我想我可能只是添加以下code到我的Global.ascx.cs文件:

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?

我有一点MVC经验,所以我道歉,如果任何这是没有意义的。

I have little experience with MVC so I apologize if any of this does not make sense.

推荐答案

看起来像最有趣的位在nopcommerce源$ C ​​$ C。默认路由被注册为

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


}

第一路径可能甚至是必要的。我不确定。从未与nopcommerce工作。

the first route may not even be necessary. i'm not sure. never worked with nopcommerce.

这篇关于如何在一个MVC应用程序中设置默认页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆