Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC6: Setting webroot programmatically

In ASP.NET MVC6 static files are served from wwwroot by default.

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

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

I would like to switch webroot at runtime depending on running in debug or production mode. Since my static assests (JS, CSS ...) are processed at build time (concatenation, minification ...) I figure it is the best approach to have a directory with the source assets and a directory that contains the optimized assets (build output).

At runtime I would like to point webroot to the optimized assets when running in production mode.

Has anybody figured out how to set webroot programmatically?

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

like image 508
jbandi Avatar asked Sep 26 '22 08:09

jbandi


1 Answers

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

The solution for my problem was to switch the directory from which to serve static files based on the hosting environment like this:

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 setup I can place all my frontend assets (html, javascript, css) in wwwsrc and serve them unprocessed during development from that directory.

My frontend build (based on gulp) then processes the assets in wwwsrc and puts the optimized assets (concatenated, minimized, revisioned and references adjusted in the html) in wwwroot.

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

Switching between debug and release build can be accomplished by setting the environment variable ASPNET_ENV in the Debug profile (Project -> Properties -> Debug or in the file Properties/launchSettings.json).

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

like image 166
jbandi Avatar answered Sep 30 '22 08:09

jbandi