Intercept all relative paths

Is it possible to intercept all and any relative paths used in the application and delete / edit part of it before the absolute path is evaluated?

Example:

In the mvc viewport-

<%= Html.Image("~/{somefolder}/logo.png") %>

      

I want to intercept the relative path "~ / {somefolder} /logo.png" and replace "{somefolder}" with a folder obtained through some logic (database, if / else, etc.)

+2


source to share


1 answer


You can create a helper that does this.

For example...



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);
}

      

In terms of search for the URL itself, you might need it here on Stephen Walther's blog.

+2


source







All Articles