ASP.NET MVC6: setting up webroot programmatically

In ASP.NET MVC6, static files are sent by default with wwwroot .

The wwwroot directory wwwroot defined in project.json with the webroot key (as described here: http://docs.asp.net/en/latest/fundamentals/static-files.html )

I am wondering if it is possible to install this webroot at runtime (at startup) programmatically.

I would like to switch webroot at runtime depending on whether it is running in debug or production mode. Since my static evaluations (JS, CSS ...) are processed during assembly (concatenation, minimization ...), I believe that this is the best approach to creating a directory with source resources and a directory that contains optimized assets (build output).

At runtime, I would like to point webroot to optimized assets when starting in production mode.

Has anyone understood how to install webroot program code?

Update 2015-11-19: In my scenario, I would like to use only static files. As indicated in the answer, switching between different assets can be implemented using TagHelpers ( http://blogs.msdn.com/b/cdndevs/archive/2015/08/06/a-complete-guide-to-the-mvc- 6-tag-helpers.aspx ) when using html server-side rendering with Razor.

+5
source share
2 answers

The documentation describes how to set up a static file serving any directory.

The solution to my problem was to switch the directory from which static files could be loaded based on such a hosting environment:

 public class Startup { private IHostingEnvironment env; public Startup(IHostingEnvironment env) { this.env = env; } public void Configure(IApplicationBuilder app) { if (env.IsDevelopment()) { var webRootPath = env.WebRootPath; var webSrcPath = webRootPath + @"\..\wwwsrc"; app.UseDefaultFiles(new DefaultFilesOptions() { FileProvider = new PhysicalFileProvider(webSrcPath), RequestPath = new PathString("") }); app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider(webSrcPath), RequestPath = new PathString("") }); } else { app.UseDefaultFiles(); app.UseStaticFiles(); // use wwwroot } } 

With this setting, I can place all my external attributes (html, javascript, css) in wwwsrc and serve them raw during development from this directory.

My external assembly (based on gulp) then processes the assets in wwwsrc and puts optimized assets (concatenated, minimized, verified and html-adjusted links) in wwwroot .

If I want to test the release build, I can serve from wwwroot .

Switching between the debug and release builds can be done by setting the ASPNET_ENV environment ASPNET_ENV in the debug profile (Project β†’ Properties β†’ Debug or in the file Properties/launchSettings.json ).

Thanks to @Maxime Rouiller for pointing me in the right direction in his answer.

+2
source

I would go differently.

First, when you use the F5 application, it automatically sets ASPNET_ENV to Development . In Razor, you can use TagHelpers to switch your resources as follows:

 <environment names="Development"> <link rel="stylesheet" href="~/css/site1.css" /> <link rel="stylesheet" href="~/css/site2.css" /> </environment> <environment names="Staging,Production"> <link rel="stylesheet" href="~/css/site.min.css" asp-file-version="true"/> </environment> 

This will allow you to modify your resources / javascript or even HTML based on what environment you are actually working on.

The only thing missing is to install ASPNET_ENV on your computer for production and production, and it will be automatically raised. There is no need to dynamically change wwwroot .

If you really want to change the value of wwwroot , for me it will be a deployment task, not a run time.

Otherwise, check the documentation for static files that you already linked. It provides code examples on how to communicate at runtime in different folders. Perhaps what you are missing is dependent on IHostingEnvironment , which will provide you environment variables to determine which environment you are in.

+2
source

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


All Articles