一、相对路径
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); }