Today I was working on ASP.NET web application on framework 4.5; I came across a strange error which I never experienced in my past working on earlier environments of dot net framework and that is error titled
“WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for ‘jquery’. Please add a ScriptResourceMapping named jquery(case-sensitive).”
When I dig in to this issue and found the root cause of this issue and here my case study about this issue…
Unobtrusive Validation is a new concept in Framework 4.5, and this is by default enables when we create a new ASP.NET web application by adding one of the key in a Web.Config->appsettings section as below
<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />
What exactly this validation does for us
For example we will compare the HTML code generated by both configurations.
Steps to create the sample application without unobtrusive validation enabled (default for all earlier versions)
1) Open Visual studio 2012 IDE and create a new Web Application as shown below
2) Now Press F5 to start the web application.
3) Right-click on the browser page, and select View Source option to open the HTML code generated by the application.
Steps to create the sample application with unobtrusive validation enabled (default for all new version(s))
1) Now you will enable unobtrusive validation. Open Web.Config and locate ValidationSettings:UnobtrusiveValidationMode key in the AppSettings section. Set the key value to WebForms.
<appSettings> <add key="aspnet:uselegacysynchronizationcontext" value="false" /> <add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms"/> </appSettings>
2) Open the Global.asax.cs file and add the following using statement
using System.Web.SessionState; using WebFormsLab.Model; using System.Web.ModelBinding;
3) Within the Application_Start method of the Global.asax.cs file, add the following code
ScriptResourceDefinition myScriptResDef = new ScriptResourceDefinition(); myScriptResDef.Path = "~/Assets/Scripts/jquery-1.7.1.min.js"; myScriptResDef.DebugPath = "~/Assets/Scripts/jquery-1.7.1.js"; myScriptResDef.CdnPath = "http://code.jquery.com/jquery-1.7.1.min.js"; myScriptResDef.CdnDebugPath = "http://code.jquery.com/jquery-1.7.1.js"; ScriptManager.ScriptResourceMapping.AddDefinition("jquery", null, myScriptResDef);
4) Open Site.Master. Add the code below to include a ScriptManager on the page to include a script reference to the jQuery client library.
<form runat="server"> <asp:ScriptManager ID="uxScriptManagerMasterPage" runat="server" EnableCdn="False"> <Scripts> <asp:ScriptReference Name="jquery" /> </Scripts> </asp:ScriptManager>
5) Now Press F5 to start the web application.
Don’t want this feature enabled?
Simply add the following setting in your AppSettings section in your web.config if it doesn’t exist. If already exist make value as “none” as shown below
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
I Hope you got advantage of this article…
Please drop a comment if you like it…
Thanks Happy Coding 🙂