我可以删除xxxxxxAreaRegistration.cs吗?
如果我在 global.asax
文件而不是该区域的 AdminAreaRegistration.cs
文件中注册到某个区域(假设它称为 Admin)的路线,是否有任何原因我无法删除 AdminAreaRegistration.cs
文件?框架中是否有任何其他代码可能在某个阶段调用它,如果缺少它可能会引发异常?
按照评论中的要求,这里是在 global.asax 中注册区域的代码
routes.MapRoute(
"AdminAreaRoute",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "Payntbrush.Presentation.Demo.MVC3.Areas.Admin.Controllers" }
).DataTokens.Add("Area", "Admin");
routes.MapRoute(
"SiteAreaRoute",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "/|Home|Account" }, // Constraint's on the URL (second param above) {controller} segment
new string[] { "Payntbrush.Presentation.Demo.MVC3.Areas.Site.Controllers" }
).DataTokens.Add("Area", "Site");
关键是使用 DataTokens 集合,如 Phillip Haydon 的这篇优秀文章。您可以添加任意多个,但请确保将根站点区域放在显式命名的区域之后,以便 {controller}/{action}/{id}
不会捕获所有请求在其他地图路线可以之前。
If I am registering the route to an area (say it's called Admin) in the global.asax
file rather than the area's AdminAreaRegistration.cs
file, is there any reason why I can't delete the AdminAreaRegistration.cs
file? Is there any other code in the framework that may call into it at some stage which might throw an exception if it is missing?
As asked in comments, here is the code to register an Area in global.asax
routes.MapRoute(
"AdminAreaRoute",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "Payntbrush.Presentation.Demo.MVC3.Areas.Admin.Controllers" }
).DataTokens.Add("Area", "Admin");
routes.MapRoute(
"SiteAreaRoute",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "/|Home|Account" }, // Constraint's on the URL (second param above) {controller} segment
new string[] { "Payntbrush.Presentation.Demo.MVC3.Areas.Site.Controllers" }
).DataTokens.Add("Area", "Site");
The key is in using the DataTokens collection, as highlighted in this excellent post by Phillip Haydon. You can add as many as you want, but make sure you put the root site area after the explicitly named areas so that {controller}/{action}/{id}
doesn't catch all the requests before the other Map Routes can.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您能否发布您用于在 global.asax 中注册您的区域的代码?
我问的原因是,我认为如果没有在某处实现 xxxAreaRegistration.cs 类(即使在另一个看起来
路由指向一个子目录,
我希望你在某个时候得到一个错误。
它可以在没有它的情况下工作,但实际上我认为你可能只是告诉 .net 你的 一个href="http://msdn.microsoft.com/en-us/library/ee402905.aspx" rel="nofollow">AreaRegistration Class 文档 它说
虽然它没有说这是唯一的方法,但我认为可能是这样。
Could you post the code that you are using to register your area in the global.asax?
The reason that I ask, is I don't think that it is technically possible to get an area working correctly without the xxxAreaRegistration.cs class implemented somewhere (even if in another project.
It might seem like it is working without it but in reality I think you might simply telling .net that your route is pointing to a sub directory.
I would expect you to get an error at some point.
Have a look at msdn AreaRegistration Class documentation it says
while it doesn't say that its the only way, I think that might be the case.
我已经删除了这些文件,到目前为止还没有遇到任何问题。在遇到任何问题之前,我会假设一切正常。
I've deleted these files and haven't had any problems so far. I'm going to assume that it is OK until I run into any issues.