2011-12-28 49 views
2

我为模块自动注册路由并尝试为admin注册路由。 有关课程报名工作正常注册路由如何输入Admin/{controller}/{action}/{id}

public class ModulSetup : BaseModule 
{ 
    public override void Setup() 
    { 
     this.SetupViewEngine(); 
     this.SetupControllers(); 
    } 

    public override void SetupRoutes(RouteCollection routes) 
    { 
     routes.MapRoute(
      "Default", 
      string.Empty, 
      new { controller = "Home", action = "Index" }); 
    } 

    protected override void SetupViewEngine() 
    { 
     ViewEngines.Engines.Add(new CoreViewEngine()); 
    } 

    protected override void SetupControllers() 
    { 
     ControllerBuilder.Current.DefaultNamespaces.Add("SGN.Core.Controllers"); 
    } 
} 

但对于管理员,我得到404

public class AdminSetup : BaseModule 
{ 
    public override void Setup() 
    { 
     this.SetupViewEngine(); 
     this.SetupControllers(); 
    } 

    public override void SetupRoutes(RouteCollection routes) 
    { 
     routes.MapRoute(
      "Admin", 
      "Admin/{controller}/{action}/{id}", 
      new { controller = "Site", action = "Index" }); 

    protected override void SetupViewEngine() 
    { 
     ViewEngines.Engines.Add(new AdminViewEngine()); 
    } 

    protected override void SetupControllers() 
    { 
     ControllerBuilder.Current.DefaultNamespaces.Add("SGN.Core.Admin.Controllers"); 
    } 
} 

例。我有控制器类网站

namespace SGN.Core.Admin.Controllers 
{ 
using System.Web.Mvc; 
using SGN.Framework.Controller; 

public class SiteController : AdminController 
{ 
    public ActionResult Index() 
    { 
     return this.View(); 
    } 
} 
} 

,当我尝试去http://localhost:777/Admin/Site/服务器返回404 :(

UPDATE

我尝试创建AreaAwareViewEngine浩写你How to set a Default Route (To an Area) in MVC

但没有帮助

UPDATE

我试试用这个url http://localhost:777/Admin/Site/Index工作。但这不好。师父不工作。

UPDATE

我用RouteDebugger在哪里使用面积等项目检查。使用Area时添加什么。我在DataTokens中如何理解添加3个参数

命名空间= SGN.Web.Areas.Admin。,面积=管理员,UseNamespaceFallback = FALSE *

我尝试添加此参数

命名空间= SGN.Core.Admin。,面积=管理员,UseNamespaceFallback = FALSE *

但不能帮助

UPDATE

我创建的类AreaAwareViewEngine这里怎么写How to set a Default Route (To an Area) in MVC

我班AreaRegistration

namespace SGN.Web.Admin 
    { 
using System.Web.Mvc; 

public class AdminAreaRegistration : AreaRegistration 
    { 
    public override string AreaName 
    { 
     get 
     { 
      return "Admin"; 
     } 
    } 

    public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.MapRoute(
      "Admin_default", 
      "Admin/{controller}/{action}/{id}", 
      new {area="Admin", controller = "Site", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
} 
} 

并创建AdminViewEngin E从AreaAwareViewEngine

namespace SGN.Core.Admin 
{ 
using System.Web.Mvc; 

using SGN.Framework; 

public class AdminViewEngine : AreaAwareViewEngine 
{ 
    public AdminViewEngine() 
    { 
     AreaMasterLocationFormats = new[] { "~/Admin/Views/Shared/admin.cshtml" }; 

     AreaPartialViewLocationFormats = new[] { "~/Admin/Views/{1}/Partials/{0}.cshtml", "~/Admin/Menu/{0}.cshtml" }; 

     AreaViewLocationFormats = new[] { "~/Admin/Views/{1}/{0}.cshtml", "~/Admin/Menu/{0}.cshtml" }; 

     ViewLocationFormats = AreaViewLocationFormats; 
     PartialViewLocationFormats = AreaPartialViewLocationFormats; 
     MasterLocationFormats = AreaMasterLocationFormats; 
    } 

    protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) 
    { 
     return new RazorView(controllerContext, partialPath, null, false, FileExtensions); 
    } 

    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) 
    { 
     if (string.IsNullOrEmpty(masterPath)) 
     { 
      masterPath = AreaMasterLocationFormats[0]; 
     } 
     var view = new RazorView(controllerContext, viewPath, masterPath, true, FileExtensions); 
     return view; 
    } 
} 
    } 

工作,如果我去http://localhost:777/Admin/Site/,如果我去http://localhost:777/Admin

回答

0

这可能是一个简单的排序问题,不能正常工作。您的管理路线是否在Core路线之前添加?我相信它会采用第一个匹配的路线,除非您的管理路线添加到其他路线之前,我怀疑其中一个匹配,并将其重定向到管理员控制器(不存在)。你可能想看看哈克的RouteDebugger

另外,有什么原因,你不使用内置的区域功能?这似乎是一个完美的匹配。

+0

RouteCollection中Admin的路由排名第二。我无法使用RouteDebugger测试路由。因为返回404 – Greg 2011-12-28 15:40:05

+0

我不使用区域,因为我不想使用文件夹区域 – Greg 2011-12-28 17:00:33

0

首先,确保您的“管理员”路线在默认的非管理员路线之前映射。另外,我想你的管理员路线改成这样:

routes.MapRoute(
     "Admin", 
     "Admin/{controller}/{action}/{id}", 
     new { controller = "Site", action = "Index", id = UrlParameter.Optional }); 

这样一来,id路线参数有被UrlParameter.Optional的默认值。您的初始路线没有默认值id,所以它会要求它。

+0

此代码不帮助我 – Greg 2011-12-28 15:37:43

+0

@Greg将此路由放在您的默认路由之前。 – 2011-12-28 15:42:14

+0

RouteCollection中Admin的路由排名第二。 First routes.IgnoreRoute(“{resource} .axd/{* pathInfo}”); – Greg 2011-12-28 15:46:00