2011-12-12 154 views
16

我希望将我的基本网址转到某个在线商店的特定类别(如果这有所作为,请登录NopCommerce在线商店)。该类别的网址是:http://myUrl.com/c/6如何在MVC应用程序上设置默认页面?

阅读一些职位,包括斯科特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的经验,所以我很抱歉,如果这些没有任何意义。

+0

该代码发生了什么?看着当前的路线,你必须有更多的路由规则,因为它不遵循那里的命名结构。你有可能发布整个注册路线部分? –

+0

用完整的RegisterRoutes方法更新。当我转到基本URL时,它会转到之前的相同页面。 –

回答

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合作过。

0

你尝试过:

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

尽量只写这篇文章的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 
     ); 

    } 

它必须从/目录/类别设置默认页/ 6

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

+1

他正在建立在现有电子商务框架之上。他不能只删除代码。有很多在幕后进行的路线注册。这基本上会导致他的应用程序相当工作... –

1

为了避免与NopCommerce更新任何未来的冲突,我会做的是创造我的主题文件夹内创建一个新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; 
     } 
    } 
} 
相关问题