Asp.Net Core Web相對路徑、絕對路徑整理

來源:互聯網
上載者:User

標籤:soft   執行環境   name   tps   方法   format   relative   針對   startup   

一、相對路徑

1.關於Asp.Net Core中的相對路徑主要包括兩個部分:一、Web根目錄,即當前網站的目錄為基礎;二、內容目錄wwwroot檔案夾,對於靜態檔案都放在這個目錄。

2.擷取控制器,Action的路徑

對於控制器、視圖的連結產生,主要通過視圖上下文、控制器內容相關的Url對象

Url對象實現了IUrlHelper介面,主要功能是擷取網站的相對目錄,也可以將‘~’發號開頭的轉換成相對目錄。

    //    // 摘要:    //     Defines the contract for the helper to build URLs for ASP.NET MVC within an application.    public interface IUrlHelper    {        //        // 摘要:        //     Gets the Microsoft.AspNetCore.Mvc.IUrlHelper.ActionContext for the current request.        ActionContext ActionContext { get; }        //        // 摘要:        //     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 are non-null.        //        // 參數:        //   actionContext:        //     The context object for the generated URLs for an action method.        //        // 返回結果:        //     The generated URL.        string Action(UrlActionContext actionContext);        //        // 摘要:        //     Converts a virtual (relative) path to an application absolute path.        //        // 參數:        //   contentPath:        //     The virtual path of the content.        //        // 返回結果:        //     The application absolute path.        //        // 備忘:        //     If the specified content path does not start with the tilde (~) character, this        //     method returns contentPath unchanged.        string Content(string contentPath);        //        // 摘要:        //     Returns a value that indicates whether the URL is local. A URL is considered        //     local if it does not have a host / authority part and it has an absolute path.        //     URLs using virtual paths (‘~/‘) are also local.        //        // 參數:        //   url:        //     The URL.        //        // 返回結果:        //     true if the URL is local; otherwise, false.        bool IsLocalUrl(string url);        //        // 摘要:        //     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.        //        // 參數:        //   routeName:        //     The name of the route that is used to generate URL.        //        //   values:        //     An object that contains route values.        //        // 返回結果:        //     The generated absolute URL.        string Link(string routeName, object values);        //        // 摘要:        //     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.        //        // 參數:        //   routeContext:        //     The context object for the generated URLs for a route.        //        // 返回結果:        //     The generated URL.        string RouteUrl(UrlRouteContext routeContext);    }
View Code

使用樣本:

<p>  ~轉相對目錄:  @Url.Content("~/test/one")</p>

輸出:/test/one

3.擷取當前請求的相對路徑

1.在Asp.Net Core中請求路徑資訊對象為PathString 對象

註:改對象沒有目前沒有絕對路徑相關資訊。

<p>    @{        PathString _path = this.Context.Request.Path;        //擷取當前請求的相對位址        this.Write(_path.Value);    }</p>

輸出:/path

2.擷取當前視圖的相對路徑

註:視圖上下文中的Path對象就是當前視圖的相對位置,string類型

<p> 當前視圖的相對目錄:   @Path</p>

輸出:/Views/Path/Index.cshtml

二、擷取絕對路徑

HostingEnvironment是承載應用當前執行環境的描述,它是對所有實現了IHostingEnvironment介面的所有類型以及對應對象的統稱。

如下面的程式碼片段所示,一個HostingEnvironment對象承載的執行環境的描述資訊體現在定義這個介面的6個屬性上。ApplicationName和EnvironmentName分別代表當前應用的名稱和執行環境的名稱。WebRootPath和ContentRootPath是指向兩個根目錄的路徑,前者指向的目錄用於存放可供外界通過HTTP請求訪問的資源,後者指向的目錄存放的則是應用自身內部所需的資源。至於這個介面的ContentRootFileProvider和WebRootFileProvider屬性返回的則是針對這兩個目錄的FileProvider對象。如下所示的HostingEnvironment類型是對IHostingEnvironment介面的預設實現。

更多參考:http://www.cnblogs.com/artech/p/hosting-environment.html

    //    // 摘要:    //     Provides information about the web hosting environment an application is running    //     in.    public interface IHostingEnvironment    {        //        // 摘要:        //     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.        string EnvironmentName { get; set; }        //        // 摘要:        //     Gets or sets the name of the application. This property is automatically set        //     by the host to the assembly containing the application entry point.        string ApplicationName { get; set; }        //        // 摘要: wwwroot目錄的絕對目錄        string WebRootPath { get; set; }        //        // 摘要:        //     Gets or sets an Microsoft.Extensions.FileProviders.IFileProvider pointing at        //     Microsoft.AspNetCore.Hosting.IHostingEnvironment.WebRootPath.        IFileProvider WebRootFileProvider { get; set; }        //        // 摘要:當前網站根目錄絕對路徑        string ContentRootPath { get; set; }        //        // 摘要:        //     Gets or sets an Microsoft.Extensions.FileProviders.IFileProvider pointing at        //     Microsoft.AspNetCore.Hosting.IHostingEnvironment.ContentRootPath.        IFileProvider ContentRootFileProvider { get; set; }    }

擷取當前網站根目錄絕對路徑,設定任何地方可以使用:

1.定義全域靜態變數:

    public class TestOne    {        public static IHostingEnvironment HostEnv;    }

2.在開機檔案Startup中賦值:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider svp){    TestOne.ServiceProvider = svp;    TestOne.HostEnv = env;}

3.輸出根目錄資訊:

<p>    @{         string json = Newtonsoft.Json.JsonConvert.SerializeObject(TestOne.HostEnv);        this.Write(json);        <script>            console.info(@Html.Raw(json));        </script>    }</p>

結果:

 

三、相對路徑轉絕對路徑

註:目前沒有找到直接轉換的方法,但是網站根目錄絕對路徑+相對路徑,就是視圖或靜態檔案的絕對路徑。可以自己封裝一下。

<p>    @{        //擷取當前視圖的絕對路徑        string viewPath = TestOne.HostEnv.ContentRootPath + Path;        this.Write(viewPath);    }</p>

輸出:F:\SolutionSet\CoreSolution\Core_2.1\Core_Ng_2.1/Views/Path/Index.cshtml,可以直接存取到檔案。

更多:

.Net Core Bitmap位元影像處理

Asp.Net Core 檔案上傳處理

Asp.Net Core擷取當前上線文對象

Asp.Net Core Web相對路徑、絕對路徑整理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.