是否可以截取应用程序中使用的所有和任何相对路径,并在评估绝对路径之前删除/编辑它的一部分?拦截所有相关路径
实施例:
在MVC视图PAGE-
<%= Html.Image("~/{somefolder}/logo.png") %>
我想和一个文件夹来拦截的相对路径 “〜/ {somefolder} /logo.png” 和替换 “{} somefolder”通过某些逻辑(数据库,if/else等)检索到的位置
是否可以截取应用程序中使用的所有和任何相对路径,并在评估绝对路径之前删除/编辑它的一部分?拦截所有相关路径
实施例:
在MVC视图PAGE-
<%= Html.Image("~/{somefolder}/logo.png") %>
我想和一个文件夹来拦截的相对路径 “〜/ {somefolder} /logo.png” 和替换 “{} somefolder”通过某些逻辑(数据库,if/else等)检索到的位置
您可以创建一个帮助程序来完成此操作。
例如...
public static string LinkedImage(this HtmlHelper html, string url)
{
Regex regex = new Regex("({(.*?)})");//This is off the top of my head regex which will get strings between the curly brackets/chicken lips/whatever! :).
var matches = regex.Matches(url);
foreach (var match in matches)
{
//Go get your value from the db or elsewhere
string newValueFromElsewhere = GetMyValue(match);
url = url.Replace(string.Format("{{0}}", match), newValueFromElsewhere);
}
return html.Image(url);
}
在寻找解决URL本身而言,你可能想看看here在斯蒂芬·瓦尔特的博客。
我希望能够在任何相对的“〜/”路径URL中替换{somefolder}。 从你链接的文章中,如果我能以某种方式重写VirtualPathUtility类(假设所有事情都经过了这个),我可以轻松地做到这一点。我会再看看它。 – Omar 2009-09-26 01:33:41
你想在哪一点做这个拦截?显示链接到这些资产的页面时?或者当请求进入Web服务器? – 2009-09-20 14:07:58
在页面完全输出给访问者之前,任何url中的“{somefolder}”的任何和所有实例都将被我想要的东西替换。我认为这将需要完成时,相对url解析为绝对的,服务器端,但我不知道。 – Omar 2009-09-21 01:18:08