views:

102

answers:

1

Is it possible to intercept all and any relative paths used in an application and remove/edit a portion of it before the absolute path is evaluated?

Example:

In mvc view page-

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

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

+2  A: 

You could create a helper that does that.

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 looking to resolve the url itself, you might want to look here at Stephen Walther's blog.

Dan Atkinson
I want to be able to replace {somefolder} in any relative "~/" path URL.From what the article you linked, if I can somehow override the VirtualPathUtility class (assuming everything goes through that) I could easily do it. I'll look into it some more.
Baddie