aeroson

Untitled

Sep 30th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. using NewControlPanel.Data;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Web;
  8. using System.Web.Http;
  9. using System.Web.Mvc;
  10. using System.Web.Optimization;
  11. using System.Web.Routing;
  12. using System.Web.SessionState;
  13.  
  14. namespace NewControlPanel
  15. {
  16. public class MvcApplication : System.Web.HttpApplication
  17. {
  18. public class MyControllerFactory : IControllerFactory
  19. {
  20. public IController CreateController(RequestContext requestContext, string controllerName)
  21. {
  22. var paths = new[]
  23. {
  24. $"{nameof(NewControlPanel)}.{nameof(Process)}.{controllerName}.Process",
  25. $"{nameof(NewControlPanel)}.{nameof(Process)}.{controllerName}.{controllerName}",
  26. $"{nameof(NewControlPanel)}.{nameof(Process)}.{controllerName}.{controllerName}Controller",
  27. $"{nameof(NewControlPanel)}.Controllers.{controllerName}",
  28. $"{nameof(NewControlPanel)}.Controllers.{controllerName}Controller",
  29. };
  30. var type = TryFind(paths);
  31.  
  32. if (type == null)
  33. {
  34. var e = CreateController(requestContext, "home");
  35. var m = e as MyController;
  36. if (m != null)
  37. {
  38. m.AddError($"Controller for {nameof(controllerName)}:{controllerName} not found, reverted to home controller");
  39. }
  40. return e;
  41. }
  42.  
  43. var c = Activator.CreateInstance(type) as IController;
  44. //c.Execute(requestContext);
  45. return c;
  46. }
  47.  
  48. public Type TryFind(params string[] typeNames)
  49. {
  50. foreach (var typeName in typeNames)
  51. {
  52. foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
  53. {
  54. var type = a.GetType(typeName, false, true);
  55. if (type != null) return type;
  56. }
  57. }
  58. return null;
  59. }
  60.  
  61. public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
  62. {
  63. return SessionStateBehavior.Default;
  64. }
  65.  
  66. public void ReleaseController(IController controller)
  67. {
  68. }
  69. }
  70.  
  71. string[] paths = new[]
  72. {
  73. // {0} is target view name, {1} - controller name and {2} - area name.
  74. "~/Process/{1}/{0}.cshtml",
  75. "~/Process/Shared/{0}.cshtml",
  76. "~/Process/Home/{0}.cshtml",
  77. };
  78.  
  79. protected void Application_Start()
  80. {
  81. Data.MyDbContext.Init();
  82.  
  83. AreaRegistration.RegisterAllAreas();
  84. GlobalConfiguration.Configure(WebApiConfig.Register);
  85. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  86. RouteConfig.RegisterRoutes(RouteTable.Routes);
  87. BundleConfig.RegisterBundles(BundleTable.Bundles);
  88.  
  89. ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
  90.  
  91. ViewEngines.Engines.Clear();
  92. ViewEngines.Engines.Clear();
  93. var razorEngine = new RazorViewEngine();
  94. razorEngine.MasterLocationFormats = razorEngine.MasterLocationFormats.Concat(paths).ToArray();
  95. razorEngine.ViewLocationFormats = razorEngine.ViewLocationFormats.Concat(paths).ToArray();
  96. razorEngine.PartialViewLocationFormats = razorEngine.PartialViewLocationFormats.Concat(paths).ToArray();
  97. ViewEngines.Engines.Add(razorEngine);
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment