Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using NewControlPanel.Data;
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Reflection;
- using System.Web;
- using System.Web.Http;
- using System.Web.Mvc;
- using System.Web.Optimization;
- using System.Web.Routing;
- using System.Web.SessionState;
- namespace NewControlPanel
- {
- public class MvcApplication : System.Web.HttpApplication
- {
- public class MyControllerFactory : IControllerFactory
- {
- public IController CreateController(RequestContext requestContext, string controllerName)
- {
- var paths = new[]
- {
- $"{nameof(NewControlPanel)}.{nameof(Process)}.{controllerName}.Process",
- $"{nameof(NewControlPanel)}.{nameof(Process)}.{controllerName}.{controllerName}",
- $"{nameof(NewControlPanel)}.{nameof(Process)}.{controllerName}.{controllerName}Controller",
- $"{nameof(NewControlPanel)}.Controllers.{controllerName}",
- $"{nameof(NewControlPanel)}.Controllers.{controllerName}Controller",
- };
- var type = TryFind(paths);
- if (type == null)
- {
- var e = CreateController(requestContext, "home");
- var m = e as MyController;
- if (m != null)
- {
- m.AddError($"Controller for {nameof(controllerName)}:{controllerName} not found, reverted to home controller");
- }
- return e;
- }
- var c = Activator.CreateInstance(type) as IController;
- //c.Execute(requestContext);
- return c;
- }
- public Type TryFind(params string[] typeNames)
- {
- foreach (var typeName in typeNames)
- {
- foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
- {
- var type = a.GetType(typeName, false, true);
- if (type != null) return type;
- }
- }
- return null;
- }
- public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
- {
- return SessionStateBehavior.Default;
- }
- public void ReleaseController(IController controller)
- {
- }
- }
- string[] paths = new[]
- {
- // {0} is target view name, {1} - controller name and {2} - area name.
- "~/Process/{1}/{0}.cshtml",
- "~/Process/Shared/{0}.cshtml",
- "~/Process/Home/{0}.cshtml",
- };
- protected void Application_Start()
- {
- Data.MyDbContext.Init();
- AreaRegistration.RegisterAllAreas();
- GlobalConfiguration.Configure(WebApiConfig.Register);
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
- ViewEngines.Engines.Clear();
- ViewEngines.Engines.Clear();
- var razorEngine = new RazorViewEngine();
- razorEngine.MasterLocationFormats = razorEngine.MasterLocationFormats.Concat(paths).ToArray();
- razorEngine.ViewLocationFormats = razorEngine.ViewLocationFormats.Concat(paths).ToArray();
- razorEngine.PartialViewLocationFormats = razorEngine.PartialViewLocationFormats.Concat(paths).ToArray();
- ViewEngines.Engines.Add(razorEngine);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment