1

In a ASP.NET MVC 4 project, I'd like to reference a versioned script file like this:

// Just some pseudo-code:
<script src="@Latest("~/Scripts/jquery-{0}.min.js")"></script>

// Resolves to the currently referenced script file
<script src="/Scripts/jquery-1.10.2.min.js"></script>

so that when a new Script version is updated via NuGet, the reference is updated automatically. I know of the bundling-and-minification feature, but it's just to much. I just want the little part which resolves the wildcards. My files are already minified, and also I don't want the bundles.

Do you have some smart ideas how to solve this?

4
  • 2
    use the bundles , create a new bundle with one file in it (jquery), and that's it. Commented Sep 25, 2013 at 13:36
  • @Aviatrix this seems pragmatic but i think it doesn't make much sense to me to use a feature called "bundling and minification" without any need for the actual bundles and nothing to minify. Then that's a quite overhead, isn't it? Commented Sep 25, 2013 at 13:47
  • 1
    It's already there if you use it or not. And i suspect there is internal caching for this , so the overhead should be close to none. you always have the option to write the tool you need :) Commented Sep 25, 2013 at 15:01
  • I think I'll have a look at it, I'm curious if there's a fairly simple way to, for example, scan one's Scripts directory. Commented Sep 25, 2013 at 15:59

1 Answer 1

5

Even though it's a little over kill to use the Bundling in MVC, but I think that will be your best bet. It's already been done and proven so why spend more time to write some proprietary code.

That being said, if you want a simple sample of what you can do, then you can try the following.

public static class Util
{
    private const string _scriptFolder = "Scripts";

    public static string GetScripts(string expression)
    {
        var path = HttpRuntime.AppDomainAppPath;
        var files = Directory.GetFiles(path + _scriptFolder).Select(x => Path.GetFileName(x)).ToList();
        string script = string.Empty;
        expression = expression.Replace(".", @"\.").Replace("{0}", "(\\d+\\.?)+");
        Regex r = new Regex(@expression, RegexOptions.IgnoreCase);

        foreach (var f in files)
        {
            Match m = r.Match(f);
            while (m.Success)
            {
                script = m.Captures[0].ToString();

                m = m.NextMatch();
            }
        }

        return script;
    }
} 

This will return you the last match in your Scripts director or it will return empty string.

Using this call

@Html.Raw(MvcApplication1.Util.GetScripts("jquery-{0}.min.js"))

Will get you this result if 1.8.2 is the last file that matched your string.

jquery-1.8.2.min.js

Hope this will help you get started.

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

1 Comment

Excellent, thank you for providing both, your personal opinion and advice but also a solution script.

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.