WebResource.axd给出了404

时间:2011-03-02 08:03:37

标签: asp.net webresource.axd

我刚在codeproject上发现an article,这让我很感兴趣...所以我在这里实现了这个:

我有两个项目:

  • MyComponent.Web(包含所有资源和控件)
  • MyComponent.Web.Demo(只是webProject)

MyComponent.Web 我有

的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
}

另外我的脚本文件的构建操作设置为Embedded Resource

MyComponent.Web.Demo我有

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>

但是WebResource.axd给了我一个404,而Reflector告诉我,我已经正确地嵌入了资源 - 所以我在这里做错了什么?

修改 您可以下载a demo here

1 个答案:

答案 0 :(得分:0)

令人讨厌的是这一个:

this.Page.RegisterJQuery();

结合

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

page.ClientScript.GetWebResourceUrl()使用pageType的程序集来搜索资源。由于我已将资源放在另一个程序集中,因此无法找到它。

所以,解决方案:

<强> 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);
    }
}