25

我在 ASP.NET 3.5 (AJAX) Web 应用程序内的特定 WebResource.axd调用上收到404 HTTP 状态错误(未找到) 。我猜这个错误是因为 bin 文件夹/GAC 中缺少特定的引用程序集而引发的。但我不知道是哪个,因为请求资源的页面非常复杂(我使用的是第三方控件和 ASP.NET Ajax。)

是否可以从查询的加密“d”查询字符串参数中知道,例如:

.../WebResource.axd?d=...

哪个程序集应该创建内容并且可能丢失?

注意:还有其他成功执行的 WebRequest.axd 调用。

4

4 回答 4

32

此问题的原因之一是注册的嵌入式资源路径不正确或资源不存在。确保将资源文件添加为Embedded Resource

Asp.net 使用WebResourceAttribute,您必须提供资源的路径。

资源文件需要作为嵌入式资源添加到项目中,并且它的路径将是完整的命名空间加上文件名。

因此,您在项目“MyAssembly”中有以下项目资源“my.js”,资源路径将是“MyAssembly.my.js”。

要检查 Web 资源处理程序未找到哪个文件,您可以解密 WebResource.axd url 上提供的哈希码。请参阅下面的示例以及如何执行此操作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            byte[] encryptedData = HttpServerUtility.UrlTokenDecode("encoded hash value");

            Type machineKeySection = typeof(System.Web.Configuration.MachineKeySection);
            Type[] paramTypes = new Type[] { typeof(bool), typeof(byte[]), typeof(byte[]), typeof(int), typeof(int) };
            MethodInfo encryptOrDecryptData = machineKeySection.GetMethod("EncryptOrDecryptData", BindingFlags.Static | BindingFlags.NonPublic, null, paramTypes, null);

            try
            {
                byte[] decryptedData = (byte[])encryptOrDecryptData.Invoke(null, new object[] { false, encryptedData, null, 0, encryptedData.Length });
                string decrypted = System.Text.Encoding.UTF8.GetString(decryptedData);

                decryptedLabel.Text = decrypted;
            }
            catch (TargetInvocationException)
            {
                decryptedLabel.Text = "Error decrypting data. Are you running your page on the same server and inside the same application as the web resource URL that was generated?";
            } 
        }
    }
}

Telerik UI 的 ASP.NET AJAX 团队链接的原始代码示例:http: //blogs.telerik.com/aspnet-ajax/posts/07-03-27/debugging-asp-net-2-0-web-resources-解密-the-url-and-getting-the-resource-name.aspx

这应该返回 aspt.net 认为嵌入资源所在的 URL 路径。

于 2009-01-12T12:50:52.557 回答
11

我只是在一个类似的问题上花了几个小时。由于 Diadistis 指出的精彩文章,我能够解密 WebResource url,并发现我的 WebResource 被翻译成错误的程序集指针,可以通过资源名称前面的垃圾识别。经过多次努力,我发现这是因为我在派生自另一个类的类中使用 Page.ClientScript.GetWebResourceUrl,该类派生于我的资源所在的程序集之外。令人困惑的是,我的类在同一个程序集中,不过派生的类不是。this.GetType() 参数许多文章状态是必须的,结果在我的情况下根本不是必须的。实际上,它需要替换为 typeof() 并且它有效!希望这可以防止其他人像我从这个臭虫那里得到同样的头痛。

于 2009-12-11T21:51:26.267 回答
11

就我而言,404 错误的根源是运行 IIS 的机器的日期和时间错误(从过去开始)。

于 2012-03-09T11:15:29.350 回答
2

您的项目是否缺少任何参考资料?

是否有任何设置为 CopyLocal=False(与 Infragistics 或 GAC'ed refs 共同)的引用无法到达目的地?

诸如反射器或依赖项遍历器之类的实用程序会告诉您您的主程序集是否缺少任何不是立即显而易见的依赖项。

global.asax 中的Application_Error处理程序是否有产生任何错误信息 (FileNotFoundExceptions) 的捕获?

您是否将自定义错误设置为“仅限远程”并从本地计算机浏览该站点?

于 2009-01-12T12:52:12.680 回答