我为模块自动注册路由并尝试为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