【发布时间】:2016-03-25 07:45:01
【问题描述】:
MVC 中的所有内置框架方法,如 Url.Action 或 Url.Content 在生成的 html 中仅生成相对 URL。许多 SO 答案提供了获取绝对路径的方法。 For example:
using System;
using System.Web;
using System.Web.Mvc;
public static class UrlExtensions
{
public static string Content(this UrlHelper urlHelper, string contentPath, bool toAbsolute = false)
{
var path = urlHelper.Content(contentPath);
var url = new Uri(HttpContext.Current.Request.Url, path);
return toAbsolute ? url.AbsoluteUri : path;
}
}
为我工作。
我的问题是,有没有理由不使用绝对路径?在使您的网站对网络抓取、可访问性工具、RSS 提要等更加开放方面,这绝对是更好的选择。有缺点吗?似乎绝对路径应该是默认的,而相对路径应该是自定义实现。
【问题讨论】:
标签: c# asp.net-mvc relative-path