WebResource.axd 给出 404

发布于 2024-10-19 15:11:52 字数 2704 浏览 1 评论 0原文

我刚刚发现一篇关于codeproject的文章,这让我很感兴趣......所以我在这里实现了这个:

我有两个项目:

  • 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向我展示,我已经正确嵌入了资源 - 那么我在这里做错了什么?

编辑 您可以此处下载演示

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水溶 2024-10-26 15:11:52

令人讨厌的事情是这里:

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文