ASP.NET: WebResource.axd call 404 error: how to know which assembly/resource is missing or responsible?

折月煮酒 提交于 2019-11-28 19:16:04
Diadistis

One of the reasons for this issue is that the registered embedded resource path is incorrect or the resource is not there. Make sure the resource file is added as a Embedded Resource.

Asp.net uses the WebResourceAttribute which you must give the path to the resource.

The resource file needs to be added as a Embedded Resource to the project and the path to it would be the full namespace plus the file name.

So you have the following project resource "my.js" in the project "MyAssembly" the resource path would be "MyAssembly.my.js".

To check what file the web resource handler is not finding you can decrypt the hash code provided on the WebResource.axd url. Please see the example below an how to do that.

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?";
            } 
        }
    }
}

Original code example by Telerik UI for ASP.NET AJAX Team Link: http://blogs.telerik.com/aspnet-ajax/posts/07-03-27/debugging-asp-net-2-0-web-resources-decrypting-the-url-and-getting-the-resource-name.aspx

This should return the URL path that aspt.net believes the embedded resource is at.

I just spent hours on a similar issue. Due to the great article pointed out by Diadistis I was able to decrypt the WebResource url and find out that my WebResource was translated into a wrong assembly pointer, recognizable by the garbage in front of your resource name. After many struggles I found out that this was because I was using the Page.ClientScript.GetWebResourceUrl in a class deriving from another class which resided outside of the assembly my resource was in. Confusing thing was that my class WAS in the same assembly, though the class deriving from was NOT. The this.GetType() parameter many articles state is a must, turned out not to be so much of a must at all in my situation. Actually, it needed to be replaced with a typeof() and it worked! Hope this may prevent others from getting the same headache as I got from this bugger.

In my case, the source of the 404 error was that the date and time of the machine running IIS were wrong (from the past).

Is your project missing any references?

Are there any references set to CopyLocal=False (common with Infragistics or GAC'ed refs) that dont make it to the destination?

A utility like reflector or dependency walker will tell you if your main assembly is missing any dependencies that are not immediately obvious.

Does the Application_Error handler in global.asax have a catch that is producing any error info (FileNotFoundExceptions)?

Did you set custom errors to 'remote only' and browse the site from the local machine?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!