开发者

Using embedded WebResources throughout Webresource.axd

The question's simple开发者_如何学C: how could one use embedded resources in asp.net applications? What are the steps to include a resource in the assembly, and how to reference it? What are the gotchas that could be encountered?


Edit: For a version without referencing Page and ClientScript, see What is the right way to handle Embedded Resources on a Razor View?

After spending a half of a day I've learned these:

  1. to embed a resource one needs to set it's Build Action to Embedded Resource (in VS Solution Explorer rightclick the file -> Properties)

  2. next AsssemblyInfo.vb must be modified to make this resources available for WebResource queries. Add [Assembly: System.Web.UI.WebResource("MyWebResourceProj.Test.css", "text/css")] to AssemblyInfo.vb located in MyProject folder of the project.

    • The name consists of root namespace/assembly name +'.'+filename. To be 100% sure of the name, use the following code snippet to look it up:
      Dim resNames = Assembly.LoadFile("YourDll.dll").GetManifestResourceNames()
    • Note that the assembly's Root Namespace must be the same as the Assembly Name (this took me about 4 hours to realize. At least with .Net v4 that is the case)
    • If there are references inside the css ( <%=WebResource("NS.image.jpg")%> ) than pass PerformSubstitution:=true for that css's WebResource attribute.
  3. Referencing the resource can be done with Page.ClientScript.GetWebResourceUrl(GetType(MyWebResourceProj.ConssumingPage), "MyWebResourceProj.Test.css")

    • Note that instead of GetType(Typename) one could use Me.GetType(), but again, that won't work if the class is inherited, so beware!

Resources:

  • Debugging ASP.NET 2.0 Web Resources: Decrypting the URL and Getting the Resource Name


Using embedded resources through WebResource.axd is a pain in the neck, as you can see from your own answer. You have to keep assemblyinfo.vb|cs in sync, and it always seems damn near impossible to get all the namespace & assembly names right in all the right places.

When you finally get it to work, your reward is an include script line that that looks like a core memory dump.

I suggest an alternative. Write yourself a very simple web handler (e.g. MyResourceLoader.ashx. Then add a method to your class that simply serves it's own embedded resources, in whatever way you think is meaningful. You can use reflection to get the classes, like WebResource does, or just hardcode whatever you need into your loader, if it's just for a specific purpose. A public method in your class might look like:

public static Stream GetResource(string resourceName) {
   // get the resource from myself, which is easy and doesn't require 
   // anything in assemblyinfo, and return it as a stream. As a bonus, 
   // you can parse it dynamically or even return things that aren't
   // just embedded, but generated completely in code!
}

Or if you decide to make something more general purpose, you can get all fancy and return more data using a class, e.g.

class ResourceInfo
{
    public Stream Data;
    public string MimeType;
    public string FileName;
}

Now you have the ability to serve up your embedded resources any way you want, e.g.

<script language="javascript" src="/MyResourceLoader.ashx/MyControlScript.js">

I think MS made a mess of that WebResource business. Luckily its' pretty straightforward to do your own thing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜