2011-12-28 24 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