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


一、相對路徑

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個屬性上。ApplicationNameEnvironmentName分別代表當前應用的名稱和執行環境的名稱。WebRootPathContentRootPath是指向兩個根目錄的路徑,前者指向的目錄用於存放可供外界通過HTTP請求訪問的資源,后者指向的目錄存放的則是應用自身內部所需的資源。至於這個接口的ContentRootFileProviderWebRootFileProvider屬性返回的則是針對這兩個目錄的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獲取當前上線文對象


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM