Asp.Net Core Web相对路径、绝对路径整理

时间:2021-12-01 12:09:18

一、相对路径

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);
}

使用示例:

<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>

结果:

Asp.Net Core Web相对路径、绝对路径整理

三、相对路径转绝对路径

注:目前没有找到直接转换的方法,但是网站根目录绝对路径+相对路径,就是视图或静态文件的绝对路径。可以自己封装一下。

<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获取当前上线文对象