2

I just found an article on codeproject, which made me quite interested ... So I've implemented this one here:

I have two projects:

  • MyComponent.Web (holding all the resources and controls)
  • MyComponent.Web.Demo (just the webProject)

In MyComponent.Web I have

AssemblyInfo.cs

[assembly: WebResource(WebResourceHelper.JQueryPath, "text/javascript")]

WebResourceHelper.cs

public static class WebResourceHelper
{
    internal const string JQueryPath = "MyComponent.Web.WebResources.jQuery.jquery-1.5.1.min.js";

    public static void RegisterJQuery(this Page page)
    {
        page.RegisterWebResource(JQueryPath);
    }

    public static void RegisterWebResource(this Page page, string path)
    {
        Contract.Requires(page != null);
        Contract.Requires(!string.IsNullOrEmpty(path));

        var pageType = page.GetType();
        var webResourcePath = page.ClientScript.GetWebResourceUrl(pageType, path);
        page.ClientScript.RegisterClientScriptResource(pageType, webResourcePath);
    }
}

TextBox.cs

// namespace: MyComponent.Web.UI.WebControls
public sealed class TextBox : System.Web.UI.WebControls.TextBox
{
    #region life cycle

    protected override void OnInit(System.EventArgs e)
    {
        base.OnInit(e);

        this.Page.RegisterJQuery();
    }

    #endregion
}

and additionally my script-file with build action set to Embedded Resource

In MyComponent.Web.Demo I have

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <myComponent:TextBox runat="server" ID="textBox" />
    </div>
    </form>
</body>
</html>

web.config

<system.web>
    <pages>
        <controls>
            <add tagPrefix="myComponent" namespace="MyComponent.Web.UI.WebControls" assembly="MyComponent.Web" />
        </controls>
    </pages>
</system.web>

But WebResource.axd gives me a 404, whereas Reflector shows me the, that I've embedded the resource correctly - so what am I doing wrong here?

Edit You can download a demo here

  • This question is similar? http://stackoverflow.com/questions/435311/asp-net-webresource-axd-call-404-error-how-to-know-which-assembly-resource-is-m – Shoban Mar 02 '11 at 08:06
  • @Shoban: nope ... the telerik tool gives me `pApp_Web_7jmvne45|MyComponent.Web.WebResources.jQuery.jquery-1.5.1.min.js` which looks correct to me ... notable: the dropDown `Select a web resource on this page:` of the telerik tool has no elements ... –  Mar 02 '11 at 08:12

1 Answers1

0

the nasty thing is this one here:

this.Page.RegisterJQuery();

combined with

var webResourcePath = page.ClientScript.GetWebResourceUrl(pageType, path);

page.ClientScript.GetWebResourceUrl() uses the assembly of pageType to search for the resource. As I've dropped the resource in another assembly instead, it can't find it.

So, the solution:

TextBox.cs

// namespace: MyComponent.Web.UI.WebControls
public sealed class TextBox : System.Web.UI.WebControls.TextBox
{
    #region life cycle

    protected override void OnInit(System.EventArgs e)
    {
        base.OnInit(e);

        this.RegisterJQuery();
    }

    #endregion
}

WebResourceHelper.cs

public static class WebResourceHelper
{
    internal const string JQueryPath = "MyComponent.Web.WebResources.jQuery.jquery-1.5.1.min.js";
    internal const string JQueryKey = "jQuery";
    private static readonly Type TypeOfWebResourceHelper = typeof (WebResourceHelper);

    public static void RegisterJQuery<TControl>(this TControl control)
        where TControl : Control
    {
        control.RegisterWebResource(JQueryKey, JQueryPath);
    }

    internal static void RegisterWebResource<TControl>(this TControl control, string key, string path)
        where TControl : Control
    {
        Contract.Requires(control != null);
        Contract.Requires(!string.IsNullOrEmpty(key));
        Contract.Requires(!string.IsNullOrEmpty(path));

        var page = control.Page;
        if (page.ClientScript.IsClientScriptIncludeRegistered(key))
        {
            return;
        }

        var webResourcePath = page.ClientScript.GetWebResourceUrl(TypeOfWebResourceHelper, path);
        page.ClientScript.RegisterClientScriptInclude(key, webResourcePath);
    }
}