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 ...
随机推荐
- University Entrace Examination zoj1023
学校招收学生 优先级按照: 分数 是否本地 志愿先后 相当于 女的开后宫 对gs进行略微修改 结束的条件为每个男的表白完所有女的 第二部分比较时 找出女的后宫里的吸引力最弱的男的比较 ...
- R语言编程艺术(4)R对数据、文件、字符串以及图形的处理
本文对应<R语言编程艺术> 第8章:数学运算与模拟: 第10章:输入与输出: 第11章:字符串操作: 第12章:绘图 =================================== ...
- Ubuntu 18.04及Snap体验——让Linux入门更简单(转))
https://www.linuxidc.com/Linux/2018-06/152993.htm 初次听说过Linux的时候,是大一计算机课时候老师介绍说除了Windows还有Linux.Unix操 ...
- vue-awesome-swiper使用纪实
最近做一个项目,需要用到轮播和全屏滑动功能,所以用到了vue-awesome-swiper插件,以下为个人记录下此插件的用法. 效果说明: 上面部分是个轮播图,自动开始轮播,轮播间隔为3000ms 推 ...
- odoo权限管理(二.记录管理)
规则保存在ir.rule模型表里,需要设置关联某个模型,关联很多组,访问权限控制和domian. 通过domain_force过滤出的一些记录来执行约束. 例子:经理只能删除状态为'cancel'的客 ...
- 用C语言的rand()和srand()产生伪随机数的方法总结
标准库<cstdlib>(被包含于<iostream>中)提供两个帮助生成伪随机数的函数: 函数一:int rand(void):从srand (seed)中指定的seed开始 ...
- 每日踩坑 2018-01-09 WebAPI会如何面对URL中的空串string参数?
这个问题是我的同事问我的,可能有点 low 哈. 同事审查我的代码,表示应该对 URL 中的 string 参数进行一个空验证. 我倾向于认为,会无法匹配到路由方法. 然后我就写了一个Test, [H ...
- bzoj1503 郁闷的出纳员
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的 工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经 ...
- enode
WeText项目:一个基于.NET实现的DDD.CQRS与微服务架构的演示案例 WeText项目:一个基于.NET实现的DDD.CQRS与微服务架构的演示案例 https://mp.weixin.qq ...
- spring cloud 学习(10) - 利用springfox集成swagger
对绝大多数程序员而言,写接口文档是一件痛苦的事情,相对文档,他们更愿意写代码.最理想的情况就是:代码即文档!服务开发完成后,部署上去文档就自动生成,没错,这就是springfox + swagger要 ...