簡體   English   中英

ASP.NET MVC 6應用程序的虛擬應用程序根路徑

[英]ASP.NET MVC 6 application's virtual application root path

如何在服務器上獲取應用程序的虛擬根路徑?

換句話說:我如何在ASP.NET MVC 6中執行以下操作?

HttpContext.Current.Request.ApplicationPath

您需要的是@Url.Content("~/") ,它將“〜”映射到您的虛擬應用程序根路徑。

看一下源代碼 ,似乎是使用HttpContext.Request.PathBase屬性:

public virtual string Content(string contentPath)
{
    if (string.IsNullOrEmpty(contentPath))
    {
        return null;
    }
    else if (contentPath[0] == '~')
    {
        var segment = new PathString(contentPath.Substring(1));
        var applicationPath = HttpContext.Request.PathBase;

        return applicationPath.Add(segment).Value;
    }

    return contentPath;
}

我在MVC Core RC2中使用以下內容:

而不是我使用的"~/something"

Context.Request.PathBase +“/ something”

或者更簡單地說,只需使用"/something" ,這意味着使用斜杠啟動路徑會告訴asp核心從根開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM