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);
}
使用示例:
<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,可以直接访问到文件。
更多:
Asp.Net Core Web相对路径、绝对路径整理的更多相关文章
- 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)
对于开发人员来说,构建一个消费应用程序时去了解各种各样的 API 是一个巨大的挑战.在你的 Web API 项目中使用 Swagger 的 .NET Core 封装 Swashbuckle 可以帮助你 ...
- 在docker中运行ASP.NET Core Web API应用程序
本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过程中,也会对docker的使用进行一些简单的描述.对于.NET Cor ...
- docker中运行ASP.NET Core Web API
在docker中运行ASP.NET Core Web API应用程序 本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过 ...
- ASP.NET Core Web开发学习笔记-1介绍篇
ASP.NET Core Web开发学习笔记-1介绍篇 给大家说声报歉,从2012年个人情感破裂的那一天,本人的51CTO,CnBlogs,Csdn,QQ,Weboo就再也没有更新过.踏实的生活(曾辞 ...
- VS 2017开发ASP.NET Core Web应用过程中发现的一个重大Bug
今天试着用VS 2017去开发一个.net core项目,想着看看.net core的开发和MVC5开发有什么区别,然后从中发现了一个VS2017的Bug. 首先,我们新建项目,ASP.NET Cor ...
- 支持多个版本的ASP.NET Core Web API
基本配置及说明 版本控制有助于及时推出功能,而不会破坏现有系统. 它还可以帮助为选定的客户提供额外的功能. API版本可以通过不同的方式完成,例如在URL中添加版本或通过自定义标头和通过Accept- ...
- Gitlab CI 自动部署 asp.net core web api 到Docker容器
为什么要写这个? 在一个系统长大的过程中会经历不断重构升级来满足商业的需求,而一个严谨的商业系统需要高效.稳定.可扩展,有时候还不得不考虑成本的问题.我希望能找到比较完整的开源解决方案来解决持续集成. ...
- 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持
HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...
- Asp.Net Core Web应用程序—探索
前言 作为一个Windows系统下的开发者,我对于Core的使用机会几乎为0,但是考虑到微软的战略规划,我觉得,Core还是有先了解起来的必要. 因为,目前微软已经搞出了两个框架了,一个是Net标准( ...
- ASP.NET Core Web API 集成测试
本文需要您了解ASP.NET Core Web API 和 xUnit的相关知识. 这里有xUnit的介绍: https://www.cnblogs.com/cgzl/p/9178672.html#t ...
随机推荐
- Windows网络命令
如何查看系统端口 Windows中要查看系统端口,可以使用netstat命令,点击开始---运行---键入cmd,打开命令提示符窗口,在命令提示符状态下键入“netstat -an”,按下回车键后就可 ...
- Codeforces Round #257 (Div. 1) D - Jzzhu and Numbers 容斥原理 + SOS dp
D - Jzzhu and Numbers 这个容斥没想出来... 我好菜啊.. f[ S ] 表示若干个数 & 的值 & S == S得 方案数, 然后用这个去容斥. 求f[ S ] ...
- docker 获取容器id
docker ps -aqf 'name=pypaltform2018_v1_trust_pro'
- 关于spark standalone模式下的executor问题
1.spark standalone模式下,worker与executor是一一对应的. 2.如果想要多个worker,那么需要修改spark-env的SPARK_WORKER_INSTANCES为2 ...
- CSS选择器优先级(转)
原文:http://www.cnblogs.com/wangfupeng1988/p/4285251.html 另外,w3c有文章介绍了CSS选择器的特定性,见https://www.w3.org/T ...
- 网络爬虫中Fiddler抓取PC端网页数据包与手机端APP数据包
1 引言 在编写网络爬虫时,第一步(也是极为关键一步)就是对网络的请求(request)和回复(response)进行分析,寻找其中的规律,然后才能通过网络爬虫进行模拟.浏览器大多也自带有调试工具可以 ...
- 3D游戏开发之UE4中的集合:TSet容器
好久没有更新了,最近一直在老家过年,网络不通的,今天才有时间更新一集. 一.TSet<T>是什么 UE4中,除了TArray动态数组外,还提供了各种各样的模板容器.这一节,我们就介绍集合容 ...
- 清北学堂省选刷题冲刺班 Test Day3
目录 2018.3.27 Test T1 T2 T3 考试代码 T2 T3 2018.3.27 Test 时间:8:00~11:30 期望得分:100+60+25=185 实际得分:100+40+25 ...
- PHP 图像居中裁剪函数
图像居中裁减的大致思路: 1.首先将图像进行缩放,使得缩放后的图像能够恰好覆盖裁减区域.(imagecopyresampled — 重采样拷贝部分图像并调整大小) 2.将缩放后的图像放置在裁减区域中间 ...
- CentOS下使用LVM进行分区(转)
说明:为什么抄,因为这篇文章图文并茂,所有测试都在CentOS 6和7测试过. 许多Linux使用者安装操作系统时都会遇到这样的困境:如何精确评估和分配各个硬盘分区的容量,如果当初评估不准确,一旦系统 ...