3

I am a newbie in asp mvc, and would like to define links in views to image/content folder in a way so I don't have to change each link if a image folder changes.

Is is possible using ActionLink and routing, bundling or there is a better way to achieve this.

I could not find a good example anywhere so I did not try anything any coding far.

I am thinking of storing a fixed path somewhere, but is that really a mvc type solution?

5
  • Do you mean if you rename the folder? Commented Aug 7, 2014 at 7:30
  • @StephenMuecke rename the folder or choose a different one. Commented Aug 7, 2014 at 7:31
  • And what does the image/content folder contain - .css files or image files (`.png, .jpg etc) - sorry I'm confused because MVC folder structure has Content and Images folders Commented Aug 7, 2014 at 7:38
  • @Would it really hurt if instead /images/ I use content/images that would store all image types ......!? Commented Aug 7, 2014 at 7:49
  • Probably not - just wanted to make sure its was for image files and not .css files (where bundling could have been a solution). Commented Aug 7, 2014 at 7:59

1 Answer 1

2

There are a number of ways you could do this. Here's one approach to extend the Url.Content() method.

1. Create an extension method

We'll called it Virtual().

namespace TestApp.Extensions
{
    public static class UrlHelperExtensions
    {
        private const string _settingPattern = "path:{0}";
        private const string _regexPattern = @"\{\w+\}";

        public static string Virtual(this UrlHelper helper, string url)
        {
            Regex r = new Regex(_regexPattern);
            var matches = r.Matches(url);

            if (matches.Count == 0) return url;

            var sb = new StringBuilder(url);
            var keys = WebConfigurationManager.AppSettings.AllKeys;

            foreach (var match in matches)
            {
                string key = match.ToString().TrimStart('{').TrimEnd('}');
                string pattern = string.Format(_settingPattern, key);

                foreach (var k in keys)
                {
                    if (k == pattern)
                    {
                        sb.Replace(match.ToString(), WebConfigurationManager.AppSettings.Get(k));
                    }
                }
            }

            return helper.Content(sb.ToString());
        }
    }
}

2. Add settings to the main Web.config

Freely add any paths you want.

<add key="path:images" value="~/Content/images" />
<add key="path:scripts" value="~/scripts" />

3. Add the namespace to the Web.config of your views directory

<namespaces>
    <add namespace="TestApp.Extensions"/>
</namespaces>

4. Use the new method

@Url.Virtual("{images}/mypic.png")

Output:

/Content/images/mypic.png

You can now use Virtual() where you would Content().

This solution is arguably excessive, but it is comprehensive.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.