Before looking at the Asp.net5, the younger brother tried to use, did a small website practice, there is a small module needs to upload pictures to wwwroot under the Images folder, but Aspnet5 has changed, before the method to obtain the root directory of the site is no longer applicable. Go online to turn over the article also did not find the method, helpless under thought to see the source (Big God don't laughed at, I also don't understand, just look at), finally let me see a class:
namespacemicrosoft.framework.runtime{[assemblyneutral] Public Interfaceiapplicationenvironment {stringApplicationbasepath {Get; } stringApplicationName {Get; } stringConfiguration {Get; } FrameworkName Runtimeframework {Get; } stringVersion {Get; } }
But how do you get the implementation class for this interface? After several attempts, a method was found:
var approot = context.applicationservices.getservice<iapplicationenvironment> (). Applicationbasepath;
According to get the application root path in stitching their own string, you can get to the folder to store pictures of the path, overjoyed Ah, but then the problem came, the site after the publication, the directory structure changed, the operation error, and then thought of Project.json This file under the first item:
This is before release: "Webroot": "Wwwroot"
This is post: "Webroot": ". /.. /.. /.. /.. /wwwroot "
This is not very simple, first get Project.json this file, and then get the node "Webroot", in the previous obtained application root path that is AppRoot splicing is not OK, the method is as follows:
var New Configuration (). Addjsonfile ("project.json"). Addenvironmentvariables (); var wwwroot=config. get<string> ("webroot");
But run up but error, do not have access to the "Project.json", some students will say directly write dead soon can be, the effect is as follows:
var approot = context.applicationservices.getservice<iapplicationenvironment> (). Applicationbasepath; var wwwroot = approot + "\\wwwroot";//Pre-release var Wwrrot = AppRoot + ". \\.. \\.. \\.. \\.. \\wwwroot ";//post-release
In fact, this also reluctantly, is a lack of flexibility, such as the wwwroot directory renamed to WWW (because the purchase of virtual space Site Directory must be www) need to be modified in the code, but there is no better way to do so.
An occasional error was reported when using the ASPNET self-hosted test:
Think of this is because the Wwwroot folder renamed to WWW, and project.config inside but did not change (or wwwroot), this is not because of the Wwwroot folder and reported the error, overjoyed Ah, clues, with anti-compilation software to see these two classes, Finally see the following code:
PublicHostingenvironment (iapplicationenvironment appenvironment, ienumerable<iconfigurehostingenvironment>configures) { This. Environmentname ="Development"; This. WebRoot =Hostingutilities.getwebroot (Appenvironment.get_applicationbasepath ()); This. Webrootfileprovider =NewPhysicalfileprovider ( This. WebRoot); using(Ienumerator<iconfigurehostingenvironment> enumerator =configures. GetEnumerator ()) { while(Enumerator. MoveNext ()) {Enumerator. Current.configure ( This); } } }
This is the sentence. WebRoot = Hostingutilities.getwebroot (Appenvironment.get_applicationbasepath ()); The problem of troubled days has finally been solved; full access to ASPNET5 website The code for the physical file root directory is as follows:
var approot = context.applicationservices.getservice<iapplicationenvironment>var Wwwroot = Microsoft.AspNet.Hosting.HostingUtilities.GetWebRoot (AppRoot);
From this end.
The first time I write a blog, not very good at expression, hope that the gods do not take offense
How to get the application root and the physical file root directory in ASP. NET 5