首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >截取所有相对路径

截取所有相对路径
EN

Stack Overflow用户
提问于 2009-09-18 19:20:00
回答 1查看 252关注 0票数 2

是否有可能截取应用程序中使用的所有和任何相对路径,并在评估绝对路径之前删除/编辑其中的一部分?

示例:

在mvc视图页面中-

代码语言:javascript
复制
<%= Html.Image("~/{somefolder}/logo.png") %>

我想截取相对路径"~/{somefolder}/logo.png“,并将"{somefolder}”替换为通过某种逻辑(数据库、if/else等)检索到的文件夹位置。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-09-18 22:53:19

你可以创建一个帮助器来做这件事。

例如..。

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1446406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档