我爲模塊自動註冊路由並嘗試爲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
RouteCollection中Admin的路由排名第二。我無法使用RouteDebugger測試路由。因爲返回404 – Greg 2011-12-28 15:40:05
我不使用區域,因爲我不想使用文件夾區域 – Greg 2011-12-28 17:00:33