是否有可能截取应用程序中使用的所有和任何相对路径,并在评估绝对路径之前删除/编辑其中的一部分?
示例:
在mvc视图页面中-
<%= Html.Image("~/{somefolder}/logo.png") %>我想截取相对路径"~/{somefolder}/logo.png“,并将"{somefolder}”替换为通过某种逻辑(数据库、if/else等)检索到的文件夹位置。
发布于 2009-09-18 22:53:19
你可以创建一个帮助器来做这件事。
例如..。
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本身而言,您可能希望在Stephen Walther的博客中查看here。
https://stackoverflow.com/questions/1446406
复制相似问题