2011-03-02 41 views
2

我剛發現an article在CodeProject上,這讓我很感興趣。所以我實現了這一個位置:WebResource.axd的給404

我有兩個項目:

  • 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給了我一個40 4,而Reflector向我展示了,我正確地嵌入了資源 - 所以我在這裏做錯了什麼?

編輯 您可以下載a demo here

+0

這個問題是否類似? http://stackoverflow.com/questions/435311/asp-net-webresource-axd-call-404-error-how-to-know-which-assembly-resource-is-m – Shoban 2011-03-02 08:06:04

+0

@Shoban:nope ... the telerik工具爲我提供了'pApp_Web_7jmvne45 | MyComponent.Web.WebResources.jQuery.jquery-1.5.1.min.js'這對我來說看起來是正確的......值得注意的是:dropDown'選擇此頁上的Web資源:'telerik工具沒有元素... – 2011-03-02 08:12:58

回答

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