Saturday, May 14, 2016

Failed to load resource: the server responded with a status of 404 (WebResource.axd)

I downloaded a GridView custom control with cool features on search and filtering that was developed on Visual Studio 2010 and ASP.NET 4.0. After playing around with the control, I decided to migrate the files to Visual Studio 2012 ASP.NET 4.5. Upon running the ASP.NET Project, the resource to be embedded on the GridView control in which case a JavaScript file was not recognized/found. And thus, returned a 404 status. After doing some research, I came up with the solution below.

Steps to fix the issue:
1. Set Build Action of the Resource/JavaScript file to Embedded Resource. In my project, the file to be embedded is EnhancedGridView.js
2. Add WebResourceAttribute to the GridView custom control to embedded a JavaScript file as Resource in an assembly. Make sure that the namespace of the file is correct.
1
2
3
4
5
[assembly: WebResource("GridViewFilter.EnhancedGridView.js", "text/javascript")]
public partial class EnhancedGridView : GridView
{
   //.....
}
3. In the control's PreRender event, I updated the namespace of where the JavaScript file is located. In my app, it's GridViewFilter which is the Root namespace.
1
2
3
4
5
6
7
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    string resourceName = "GridViewFilter.EnhancedGridView.js";
    ClientScriptManager cs = this.Page.ClientScript;
    cs.RegisterClientScriptResource(typeof(CustomGridView), resourceName);
}

References:
Walkthrough: Embedding a JavaScript File as a Resource in an Assembly
WebResourceAttribute Class

0 comments:

Post a Comment