One, relative path
1. The relative paths in the ASP. NET core include two parts: first, the Web root directory, which is the base of the current Web site, the Content Directory Wwwroot folder, for static files are placed in this directory.
2. Get the path to the Controller, action
For the controller, the link to the view is delivered, primarily through the view context, the URL object of the controller context
The URL object implements the Iurlhelper interface, the main function is to get the relative directory of the site, or you can convert the ' ~ ' to a relative directory at the beginning of the number.
// //Summary://defines the contract for the helper-to-build URLs for ASP. NET MVC within an application. Public InterfaceIurlhelper {// //Summary://Gets the Microsoft.AspNetCore.Mvc.IUrlHelper.ActionContext for the current request.Actioncontext Actioncontext {Get; } // //Summary://generates a URL with an absolute path for an action method, which contains the//action name, controller name, route values, protocol to use, host name, and fragment//specified by Microsoft.AspNetCore.Mvc.Routing.UrlActionContext. Generates an//Absolute URL if Microsoft.AspNetCore.Mvc.Routing.UrlActionContext.Protocol and//Microsoft.AspNetCore.Mvc.Routing.UrlActionContext.Host is non-null. // //Parameters://Actioncontext://The context object for the generated URLs for a action method. // //return Result://The generated URL. stringAction (Urlactioncontext actioncontext); // //Summary://converts a virtual (relative) path to an application absolute path. // //Parameters://Contentpath://The virtual path of the content. // //return Result://The application absolute path. // //remark://If The specified content path does not start with the tilde (~) character, this//method returns Contentpath unchanged. stringContent (stringContentpath); // //Summary://Returns A value that indicates whether the URL is local. A URL is considered//The local if it does not had a host/authority part and it had an absolute path. //URLs using virtual paths (' ~/') are also local. // //Parameters://URL://The URL. // //return Result://true if the URL is local; otherwise, false. BOOLIslocalurl (stringURL); // //Summary://generates an absolute URL for the specified routeName and route values, which//contains the protocol (such as "http" or "https") and host name from the current//request. // //Parameters://RouteName://The name of the route is used to generate URL. // //values://An object, that contains route values. // //return Result://The generated absolute URL. stringLink (stringRouteName,Objectvalues); // //Summary://generates a URL with an absolute path, which contains the route name, route values,//protocol to use, host name, and fragment specified by Microsoft.AspNetCore.Mvc.Routing.UrlRouteContext. //generates an absolute URL if Microsoft.AspNetCore.Mvc.Routing.UrlActionContext.Protocol//and Microsoft.AspNetCore.Mvc.Routing.UrlActionContext.Host are non-null. // //Parameters://Routecontext://The context object for the generated URLs for a route. // //return Result://The generated URL. stringRouteUrl (Urlroutecontext routecontext); }
View Code
Examples of Use:
< P > ~ Goto relative directory: @Url. Content ("~/test/one")</p>
Output:/test/one
3. Get the relative path of the current request
1. Request path Information object to PathString object in ASP.
Note: There is currently no absolute path-related information for the object being changed.
< P > @{ pathstring _path = this. Context.Request.Path; Gets the relative address of the current request this . Write (_path. Value); } </ P >
Output:/path
2. Get the relative path of the current view
Note: The path object in the view context is the relative position of the current view, String type
< P > relative contents of Current view: @Path</p>
Output:/views/path/index.cshtml
Second, get the absolute path
hostingenvironment is a description of the current execution environment that hosts the application, and it is a generic term for all the types that implement the Ihostingenvironment interface and the corresponding objects.
As shown in the following code snippet, a description of the execution environment hosted by a Hostingenvironment object is represented on the 6 attributes that define the interface. ApplicationName and environmentname represent the name of the current app and the name of the execution environment, respectively. Webrootpath and contentrootpath are paths to two root directories that are used to store resources that can be accessed by the outside world through HTTP requests, which point to a directory where the resources required within the application itself are stored. As for the Contentrootfileprovider and webrootfileprovider properties of this interface, the Fileprovider object for both directories is returned. The hostingenvironment type as shown below is the default implementation of the Ihostingenvironment interface.
More information: http://www.cnblogs.com/artech/p/hosting-environment.html
// //Summary://provides information about the web hosting environment a application is running//In . Public Interfaceihostingenvironment {// //Summary://Gets or sets the name of the environment. This property is automatically set//By the host to the value of the "aspnetcore_environment" environment variable. stringEnvironmentname {Get;Set; } // //Summary://Gets or sets the name of the application. This property is automatically set//By the host to the assembly containing the application entry point. stringApplicationName {Get;Set; } // // Abstract: Wwwroot directory of the Absolute directory string webrootpath { get; Set ; } // //Summary://Gets or sets an Microsoft.Extensions.FileProviders.IFileProvider pointing at//Microsoft.AspNetCore.Hosting.IHostingEnvironment.WebRootPath.Ifileprovider Webrootfileprovider {Get;Set; } // // Abstract: The absolute path of the current Web site root directory string contentrootpath { get; Set ; } // //Summary://Gets or sets an Microsoft.Extensions.FileProviders.IFileProvider pointing at//Microsoft.AspNetCore.Hosting.IHostingEnvironment.ContentRootPath.Ifileprovider Contentrootfileprovider {Get;Set; } }
Gets the absolute path of the current Web site root directory, which can be used anywhere:
1. Define Global static variables:
Public class Testone { publicstatic ihostingenvironment hostenv; }
2. Assign a value in startup file Startup:
Public void Configure (Iapplicationbuilder app, Ihostingenvironment env, IServiceProvider SVP) { = SVP; = env;}
3. Output root directory information:
< P > @{ String json = Newtonsoft.Json.JsonConvert.SerializeObject (testone.hostenv); This. Write (JSON); < Script > console.info (@Html. Raw (JSON)); </ Script > }</p>
Results:
Third, relative path to absolute path
Note: The direct conversion method is not currently found, but the site root absolute path + relative path is the absolute path to the view or static file. You can encapsulate it yourself.
< P > @{ //Gets the absolute path of the current view string viewPath = TestOne.HostEnv.ContentRootPath + path; This. Write (ViewPath); } </ P >
Output: f:\solutionset\coresolution\core_2.1\core_ng_2.1/views/path/index.cshtml, can be accessed directly to the file.
More:
. Net Core Bitmap Bitmap processing
ASP. NET Core File upload processing
ASP. NET core Gets the current online text object
ASP. NET Core Web-relative path, absolute path collation