asp.net core - hostfile.json webroot property not serving static files -


going nuts :-)

i've installed latest asp.net core (rc2).

i'd able create *.sln, multiple *.csproj: 1 server side development, , 1 client side development.

the reason keeping them separate can have option of giving the clientside *.csproj external developers better ui skills work on without needing know server side code. work on client side html/js using visual studio code or other light weight ide, , not requiring visual studio involved.

in client *.csproj, i'd serve static files (html/js/css) angular project from root directory, not wwwroot directory, gulpfile.js relative paths, etc identical how 1 set angular project without visual studio.

as understand it, rules now: * use webroot setting in hosting.json if hosting.json file exists. * otherwise, use wwwroot. * if that's missing, use root. * see: https://github.com/aspnet/hosting/issues/450

first, checked had set static page routing. created wwwroot/index.html page. tada! works.

now, renamed directory app/ , updated hosting.json point it. after reload of project, app/ folder changed icon..good...run...no joy. fight while. no success...

then delete app/ folder , hosting.json file altogether. end wanting throw something...

the way i'm getting static files if folder called wwwroot. whether have hosting.json file or not.

that's contrary documentation at: https://github.com/aspnet/hosting/issues/450

has else succeeded in getting rid of wwwroot folder? if so...how?!?!?

thank you!

although can open root of asp.net core app serving static files, it's not idea because once do, there's nothing preventing navigating project.json or other file in root.

that being said, here's how go serving static files outside of wwwroot.

first, create static class returns iapplicationbuilder. in here define physical path make accessible along optional url re-write of path (see comments in code):

using system.io;  using microsoft.aspnetcore.builder; using microsoft.aspnetcore.hosting.internal; using microsoft.extensions.fileproviders;  public static class applicationbuilderextensions {     public static iapplicationbuilder userootresources(this iapplicationbuilder app, hostingenvironment env)     {         //var path = path.combine(env.contentrootpath); // warning - opens root of app         var path = path.combine(env.contentrootpath, "static"); // allow serving contents in physical path folder named 'static'         var provider = new physicalfileprovider(path);          var options = new staticfileoptions();         // below line re-writes path name of physical path, can have string value of want call         options.requestpath = ""; //  in example, empty string give *appearance* of being served root         //options.requestpath = "/static"; // use url path named static, made-up name want         options.fileprovider = provider;          app.usestaticfiles(options);         return app;     } } 

next, in startup.cs, call function configure method:

app.userootresources((hostingenvironment)env); 

now can serve static files outside of wwwroot! referencing static file in html use path defined in options.requestpath set in applicationbuilderextensions class. assuming left requestpath empty string simulate root, call resource lived there (even though lives in 'static' folder) magic of url re-writing:

<img src="bus.jpg"/> 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo