ASP.Net MVC Embedded Resource Helper Class

Getting embedded resources such as JavaScript, CSS and image files is a little bit more difficult when using the ASP.Net MVC framework as most of the useful methods for this will not work.

The only reliable way I have found it to invoke the GetWebResourceUrlInternal method via reflection on the assembly.

Note: I realise there are probably better ways of doing this but I posting this as a reminder to myself more than anything.

So, put your resources in the AssemblyInfo.cs folder as usual: (the following example is AssemblyName.Folder.File).

  1. [assembly: WebResource("MvcWebResourceTest.Resources.MyJavascriptFile.js", "test/javascript")]

You will need to add the following using statement System.Web.UI to the AssemblyInfo.cs file.

Note: make sure you’ve set the files Build Action property to Embedded Resource on the MyJavascriptFile.js.

You can then use the following class to generate the resource string to call in you’re html helpers, controllers etc.

 

  1. /// <summary>
  2.     /// Helper class for getting embedded resources in ASP.Net MVC.
  3.     /// </summary>
  4.     public static class WebResourceHelper
  5.     {
  6.         const string GetWebResourceUrlInternal = "GetWebResourceUrlInternal";
  7.         /// <summary>
  8.         /// Gets the internal embedded resource url.
  9.         /// </summary>
  10.         /// <typeparam name="TAssemblyObject">
  11.         /// Accepts an object that exists within the assembly containing the resources.
  12.         /// </typeparam>
  13.         /// <param name="resourcePath">Accepts a string resource path - assembly.folder.resource name</param>
  14.         /// <returns>A string web resource url.</returns>
  15.         public static string GetResourceUrl<TAssemblyObject>(string resourcePath)
  16.         {
  17.             MethodInfo getResourceUrlMethod = typeof(AssemblyResourceLoader)
  18.                 .GetMethod(GetWebResourceUrlInternal, BindingFlags.NonPublic | BindingFlags.Static);
  19.             string resourceUrl = string.Format("/{0}", getResourceUrlMethod.Invoke
  20.                 (
  21.                     null,
  22.                     new object[] { Assembly.GetAssembly(typeof(TAssemblyObject)), resourcePath, false })
  23.                 );
  24.             return resourceUrl;
  25.         }
  26.     }

 

An example call from a controller to add the resource to the view data collection could be something like the following:

  1.             ViewData["ResourceUrl"] =
  2.                 WebResourceHelper.GetResourceUrl<HomeController>("MvcWebResourceTest.Resources.MyJavascriptFile.js");

Hope this is useful.

Kind Regards,

Sean McAlinden.

www.asp-net-mvc.com

Comments

# ASP.Net MVC Embedded Resource Helper Class

Tuesday, November 03, 2009 11:09 PM by WebDevVote.com

You are voted (great) - Trackback from WebDevVote.com

# www.asp-net-mvc.com

Wednesday, November 04, 2009 2:31 AM by Jürgen Gutsch

Soeben bin ich auf Sean McAlinden's Blog gestoßen und zwar auf eine Beitrag, in dem Sean eine kleine

# Twitter Trackbacks for ASP.Net MVC Embedded Resource Helper Class - Sean McAlinden's Blog [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 ASP.Net MVC Embedded Resource Helper Class - Sean McAlinden's Blog         [asp.net]        on Topsy.com

# re: ASP.Net MVC Embedded Resource Helper Class

Wednesday, November 04, 2009 2:21 PM by RichardD

The GetWebResourceUrlInternal method doesn't change between calls, so you can improve performance by caching the result of the reflection call.

Rather than calling Assembly.GetAssembly(type), you could just use type.Assembly, which is equivalent.

Also, your code only works if your application is in the site root.

public static class WebResourceHelper

{

private static readonly Func<Assembly, string, bool, string> GetWebResourceUrlInternal = FindMethod();

private static readonly Func<Assembly, string, bool, string> FindMethod()

{

var method = typeof(System.Web.Handlers.AssemblyResourceLoader)

.GetMethod("GetWebResourceUrlInternal", BindingFlags.Static | BindingFlags.NonPublic);

return (Func<Assembly, string, bool, string>)Delegate.CreateDelegate(

typeof(Func<Assembly, string, bool, string>),

method, true);

}

public static string GetResourceUrl(Assembly assembly, string resourcePath)

{

if (null == assembly) throw new ArgumentNullException("assembly");

if (string.IsNullOrEmpty(resourcePath)) throw new ArgumentNullException("resourcePath");

string result = GetWebResourceUrlInternal(assembly, resourcePath, false);

return VirtualPathUtility.ToAbsolute("~/" + result);

}

public static string GetResourceUrl(Type typeInAssembly, string resourcePath)

{

if (null == typeInAssembly) throw new ArgumentNullException("typeInAssembly");

return GetResourceUrl(typeInAssembly.Assembly, resourcePath);

}

public static string GetResourceUrl<TAssemblyObject>(string resourcePath)

{

return GetResourceUrl(typeof(TAssemblyObject).Assembly, resourcePath);

}

}

# re: ASP.Net MVC Embedded Resource Helper Class

Wednesday, November 04, 2009 2:45 PM by SeanMcAlinden

Hi Richard, Thanks ever so much - that's really useful - I'll definitely give that a go - it looks much better.

Cheers,

Sean.

# re: ASP.Net MVC Embedded Resource Helper Class

Saturday, March 27, 2010 10:23 AM by Saloman

Greeting. Illusions are art, for the feeling person, and it is by art that you live, if you do. Help me! Looking for sites on: Purchase turbo tax. I found only this - <a href="turbo-tax.biz/.../">turbo tax home and business</a>. Turbo tax, trying video recorder is a harsh golf to start you journey visitor people sellers. When you stand district lining, love a not adviser of the travel when you are either involved, turbo tax. Best regards :cool:, Saloman from Monaco.

# re: ASP.Net MVC Embedded Resource Helper Class

Tuesday, May 31, 2011 1:49 AM by weblogs.asp.net

Asp net mvc embedded resource helper class.. I like it :)

Leave a Comment

(required) 
(required) 
(optional)
(required)