一、相对路径

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,可以直接访问到文件。

更多:

.Net Core Bitmap位图处理

Asp.Net Core 文件上传处理

Asp.Net Core获取当前上线文对象

Asp.Net Core Web相对路径、绝对路径整理的更多相关文章

  1. 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)

    对于开发人员来说,构建一个消费应用程序时去了解各种各样的 API 是一个巨大的挑战.在你的 Web API 项目中使用 Swagger 的 .NET Core 封装 Swashbuckle 可以帮助你 ...

  2. 在docker中运行ASP.NET Core Web API应用程序

    本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过程中,也会对docker的使用进行一些简单的描述.对于.NET Cor ...

  3. docker中运行ASP.NET Core Web API

    在docker中运行ASP.NET Core Web API应用程序 本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过 ...

  4. ASP.NET Core Web开发学习笔记-1介绍篇

    ASP.NET Core Web开发学习笔记-1介绍篇 给大家说声报歉,从2012年个人情感破裂的那一天,本人的51CTO,CnBlogs,Csdn,QQ,Weboo就再也没有更新过.踏实的生活(曾辞 ...

  5. VS 2017开发ASP.NET Core Web应用过程中发现的一个重大Bug

    今天试着用VS 2017去开发一个.net core项目,想着看看.net core的开发和MVC5开发有什么区别,然后从中发现了一个VS2017的Bug. 首先,我们新建项目,ASP.NET Cor ...

  6. 支持多个版本的ASP.NET Core Web API

    基本配置及说明 版本控制有助于及时推出功能,而不会破坏现有系统. 它还可以帮助为选定的客户提供额外的功能. API版本可以通过不同的方式完成,例如在URL中添加版本或通过自定义标头和通过Accept- ...

  7. Gitlab CI 自动部署 asp.net core web api 到Docker容器

    为什么要写这个? 在一个系统长大的过程中会经历不断重构升级来满足商业的需求,而一个严谨的商业系统需要高效.稳定.可扩展,有时候还不得不考虑成本的问题.我希望能找到比较完整的开源解决方案来解决持续集成. ...

  8. 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持

    HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...

  9. Asp.Net Core Web应用程序—探索

    前言 作为一个Windows系统下的开发者,我对于Core的使用机会几乎为0,但是考虑到微软的战略规划,我觉得,Core还是有先了解起来的必要. 因为,目前微软已经搞出了两个框架了,一个是Net标准( ...

  10. ASP.NET Core Web API 集成测试

    本文需要您了解ASP.NET Core Web API 和 xUnit的相关知识. 这里有xUnit的介绍: https://www.cnblogs.com/cgzl/p/9178672.html#t ...

随机推荐

  1. 微信隐藏的webJS Api汇总

    1.右侧菜单增加"查看公众账号" API document.getElementById('post-user').addEventListener('click', functi ...

  2. Java 类的继承详解

    /*文章中用到的代码只是一部分,需要完整代码的可通过邮箱联系我1978702969@qq.com*/ 在面向对象的语言中如C++和JAVA,都有一个比较重要的机制——类的继承.这里将对JAVA中的类的 ...

  3. hdu 3033(好题,分组背包)

    I love sneakers! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. Django中的ORM关系映射查询方面

    ORM:Object-Relation Mapping:对象-关系映射 在MVC框架中的Model模块中都包括ORM,对于开发人员主要带来了如下好处: 实现了数据模型与数据库的解耦,通过简单的配置就可 ...

  5. UG中STP203和STP214的区别

    UG转档STP203,STP214的区别:STP214转出的图档将保留原图属性,例如所在图层,曲面颜色,装配组件名称等.STP203没有上述功能.

  6. 面向对象设计原则 里氏替换原则(Liskov Substitution Principle)

    里氏替换原则(Liskov Substitution Principle LSP)面向对象设计的基本原则之一. 里氏替换原则中说,任何基类可以出现的地方,子类一定可以出现. LSP是继承复用的基石,只 ...

  7. android activity 启动模式

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 1,标准的, 2,单个 顶部 3,单个 任务 4,单个 实例 标准的 就是 每启动一次这 ...

  8. JFreeChart 之饼图

    JFreeChart 之饼图 一.JFreeChart 简介 JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications, applets, ...

  9. Java并发(十八):阻塞队列BlockingQueue

    阻塞队列(BlockingQueue)是一个支持两个附加操作的队列. 这两个附加的操作是:在队列为空时,获取元素的线程会等待队列变为非空.当队列满时,存储元素的线程会等待队列可用. 阻塞队列常用于生产 ...

  10. git 撤销本地修改

    git checkout file 例如:git checkout app/views/carts/_index_m.html.erb 可以先用 git status 查看差异 然后 git chec ...