Can MVC Views be converted for multiple deployments, such as web.config files?

In one of my MVC projects, I have a special configuration setting for a test deployment site. By doing this, I was able to add a tranformation configuration to override various parameters in the web.config file. For example, I have the following files:

web.config web.release.config web.debug.config web.testsite.config 

When I deploy my test site, it now overwrites some of the parameters specified in my web.testsite.config

Is it possible to get the same behavior on some of my ideas? For example, can I have index.testsite.cshtml? I could turn on and off the behavior with flags from the configuration, however, it seems that a cleaner approach should provide for additional conversions / replacements depending on the configuration.

+4
source share
3 answers

It is really easy to do.

* global.asax - Inside Application_Start () *

 var displayModes = DisplayModeProvider.Instance.Modes; displayModes.Insert(0, new DefaultDisplayMode("TestSite") { ContextCondition = (context => IsTestSite()) }); 

IsTestSite () definition

 public bool IsTestSite() { bool isTestSite; return bool.TryParse(ConfigurationManager.AppSettings["isTestSite"], out isTestSite); } 

To do this, now your application will use Intex.TestSite.cshtml , if it is present otherwise, it will serve as Index.cshtml . The same is true for any other view name, just add TestSite before the extension.

+2
source

Add to your Base/Controller :

 protected override void OnResultExecuting(ResultExecutingContext filterContext) { var viewResult = filterContext.Result as ViewResult; if (viewResult != null) { string env = ... // determine your environment somehow var razorEngine = viewResult.ViewEngineCollection.OfType<RazorViewEngine>().Single(); var viewName = !String.IsNullOrEmpty(viewResult.ViewName) ? viewResult.ViewName : filterContext.RouteData.Values["action"].ToString(); var razorView = razorEngine.FindView(filterContext.Controller.ControllerContext, viewName, viewResult.MasterName, false).View as RazorView; var currentPath = razorView.ViewPath; var newPath = currentPath.Replace(".cshtml", env + ".cshtml"); if (razorEngine.FileExists(filterContext.Controller.ControllerContext, newPath)) viewResult.View = new RazorView(filterContext.Controller.ControllerContext, newPath, razorView.LayoutPath, razorView.RunViewStartPages, razorView.ViewStartFileExtensions); } base.OnResultExecuting(filterContext); } 

Also, if you use MVC 4 (hence WebPages 2.0), you can use DisplayModeProvider for this easily.

At Global.asax :

 protected void Application_Start() { DisplayModeProvider.Instance.Modes.Add(new DefaultDisplayMode("debug") { ContextCondition = (context => context.IsDebuggingEnabled) }); DisplayModeProvider.Instance.Modes.Add(new DefaultDisplayMode("test") { ContextCondition = (context => context.Request.IsLocal) }); } 
+1
source

You may be able to implement a custom action filter that checks your configuration parameter and serves to present correctly based on its value.

0
source

Source: https://habr.com/ru/post/1486053/


All Articles