环境:asp.net mvc3 vs2010
当使用多Area工作时,为了能在Area之间共享Shared目录,需要注册各自Shared地址。
在Global.asax.cs,需要如下添加不同Area的Engine:
- [csharp] view plaincopy
- 01.public class MvcApplication : System.Web.HttpApplication
- 02.{
- 03....
- 04.
- 05. protected void Application_Start()
- 06. {
- 07. RegisterRoutes(RouteTable.Routes);
- 08. ViewEngines.Engines.Add(new AgileViewEngine());
- 09. ViewEngines.Engines.Add(new MySpaceViewEngine());
- 10. ViewEngines.Engines.Add(new SFCViewEngine());
- 11. }
- 12.}
- 13.public class AgileViewEngine : RazorViewEngine
- 14.{
- 15. private static string[] NewPartialViewFormats = new[] {
- 16. "~/Areas/Agile/Views/Shared/{0}.cshtml",
- 17. };
- 18.
- 19. public AgileViewEngine()
- 20. {
- 21. basebase.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();
- 22. }
- 23.}
- 24.public class MySpaceViewEngine : RazorViewEngine
- 25.{
- 26. private static string[] NewPartialViewFormats = new[] {
- 27. "~/Areas/MySpace/Views/Shared/{0}.cshtml"
- 28. };
- 29.
- 30. public MySpaceViewEngine()
- 31. {
- 32. basebase.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();
- 33. }
- 34.}
- 35.public class SFCViewEngine : RazorViewEngine
- 36.{
- 37. private static string[] NewPartialViewFormats = new[] {
- 38. "~/Areas/SFC/Views/Shared/{0}.cshtml"
- 39. };
- 40.
- 41. public SFCViewEngine()
- 42. {
- 43. basebase.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();
- 44. }
- 45.}
这样,这三个Area的Views/Shared目录就可以共享了。
本文转自火星人陈勇 51CTO博客,原文链接:http://blog.51cto.com/cheny/1101218