我自动注册模块的路由并尝试注册admin的路由。 模块注册工作正常
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");
}
}
实施例。我有控制器类Site
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 :(
更新
我尝试创建AreaAwareViewEngine并写下How to set a Default Route (To an Area) in MVC
但不是帮助
更新
我尝试使用此网址http://localhost:777/Admin/Site/Index。但这并不好。师父不工作。
更新
我使用RouteDebugger来检查我使用Area的其他项目。使用区域时添加什么。我如何理解DataTokens中添加3个参数
Namespaces = SGN.Web.Areas.Admin。,area = Admin,UseNamespaceFallback = False *
我尝试添加此参数
Namespaces = SGN.Core.Admin。,area = Admin,UseNamespaceFallback = False *
但不是帮助
更新
我创建了类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 }
);
}
}
}
从AreaAwareViewEngine
创建AdminViewEnginenamespace 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 :(得分:0)
这可能是一个简单的订购问题。您的管理员路线是否在核心路线之前添加?我相信它将采用第一个匹配的路由,除非你的管理员路由被添加到其他路由之前,我怀疑其中一个是匹配的,并且它被重定向到管理员控制器(它不存在)。你可能想看看Haack的RouteDebugger。
此外,您是否有任何理由不使用内置区域功能?这似乎是一个完美的匹配。
答案 1 :(得分:0)
首先,确保在默认的非管理员路线之前映射“管理员”路线。另外,我会将您的管理员路线更改为:
routes.MapRoute(
"Admin",
"Admin/{controller}/{action}/{id}",
new { controller = "Site", action = "Index", id = UrlParameter.Optional });
这样,id
路由参数的默认值为UrlParameter.Optional
。您的初始路线没有id
的默认路线,因此需要它。