ASP.NET 2.0 Web Resources and SharePoint

One of the many new features in ASP.NET 2.0 is called Web Resources. Basically this allows you to store resources like images, scripts and so on, in an assembly. You can do this by using the WebResource attribute. This attribute can be applied on assembly level like this

[assembly: WebResource("image1.jpg", "image/jpeg")]
[assembly: WebResource("help.htm", "text/html", PerformSubstitution=true)]

If you want to use any of your resources in an ASP.NET form (or user control):

<img alt="help image" src=<% = WebResource("image1.jpg") %> />

When the page is rendered the image will point to something like
<img alt="help image" src=WebResource.axd?d=bWzAGpnwEO_jX-h7siKzTsDydsnTE8qDad/>

Many of the default ASP.NET 2.0 controls use this feature to store scripts and images, a good example is the TreeView control. If you want to use the same technique in SharePoint, for example when you want to run ASP.NET 2.0 user controls using the Son of SmartPart, you will face an issue. The problem is that the ISAPI filter of SharePoint will try to process the request to WebResource.axd, so you won’t be able to retrieve any resources. So as Hans suggested, I’m going to explain how to fix this.

To prevent that the ISAPI filter will try to process the requests to WebResource.axd, you have to exclude it as a managed path. On your SharePoint server, open up the SharePoint Central Administration site from the Administrative Tools start menu group and click the “Configure virtual server settings” link. On the next page you will see a list of all your virtual servers, click on the one that you would like to configure (you have to repeat the steps for each virtual server). Next, click the “Define managed paths” link. In the “Add a New Path” section, fill out WebResource.axd as the name of the path, for the type select “Exclude path”. Finally click the OK button. Now you can use WebResource in SharePoint sites!

6 Comments

  • Hello

    I have a question

    We want to update our SQL Server 2000 to 2005 and ASP.NET 2.0.

    How will work our SharePoint Portal after this upgdrade ?(if it will work)

    We applied WSS SP 2 and SPS SP2.

    Thank's

  • For this to work, do you need to compile the project? How do you deploy an image with the code if youre not compiling (ie, no Sharepoint on your dev box)?



    I'm getting an error saying WebResource does not exist in the current context. I've added the [assembly ... code to my cs code, and the WebResource.

  • Hi, i know that this post is about sharepoint but i can't find an answer anywhere for my problem so i decided to ask you. I'm using a compiled usercontrol that has 2 images set has embeded resources and I've also used the WebResource attribute in my UC.
    This UC among others things, should show the image embeded in the assembly but the image doesn't appear. The image is getting the url from "WebResource.axd?d=tYghds...) as it should but the image does not appear.

    So, my question is, is it possible to have a usercontrol using an image embeded? How?

    I hope you can help me on this because i can't find a solution anywhere. Thanks.

  • Jan,

    Great Post, but have you heard of the same issue occuring in non-Share Point applications? I've recently updated a VS 2003 app to VS 2005, and I get the same error everytime I post. I tried removing the axd extension from my IIS configuration, but it still occurs. Any advice?

    Thanks in advance,

    Peter

  • Jan,

    I wanted to note that I just ran into this outside the context of SharePoint. Using Fiddler and some troubleshooting, I found another thing that can cause this that I haven't seen anyone else report yet.

    I had defined a wildcard application map to:
    C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll

    In order to force everything through my custom authentication scheme (i.e. you couldn't browse directly to a resource without providing authentication first). It was a cheap way to that goal--but had the undesired side effect that you are discussing here. My solution was to remove the wildcard application mapping and seek alternatives. Anyway, I wanted to note it in case someone else runs across this in the same way.

  • Lucy! Please call me,Lucy! Please call me

Comments have been disabled for this content.