Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

In my web application I get the following error:

WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

How can I solve it?

like image 621
Oz Cohen Avatar asked May 14 '16 17:05

Oz Cohen


2 Answers

Since .NET 4.5 the Validators use data-attributes and bounded Javascript to do the validation work, so .NET expects you to add a script reference for jQuery.

There are two possible ways to solve the error:


Disable UnobtrusiveValidationMode:

Add this to web.config:

<configuration>     <appSettings>         <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />     </appSettings> </configuration> 

It will work as it worked in previous .NET versions and will just add the necessary Javascript to your page to make the validators work, instead of looking for the code in your jQuery file. This is the common solution actually.


Another solution is to register the script:

In Global.asax Application_Start add mapping to your jQuery file path:

void Application_Start(object sender, EventArgs e)  {     // Code that runs on application startup     ScriptManager.ScriptResourceMapping.AddDefinition("jquery",      new ScriptResourceDefinition     {         Path = "~/scripts/jquery-1.7.2.min.js",         DebugPath = "~/scripts/jquery-1.7.2.js",         CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1.min.js",         CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1.js"     }); } 

Some details from MSDN:

ValidationSettings:UnobtrusiveValidationMode Specifies how ASP.NET globally enables the built-in validator controls to use unobtrusive JavaScript for client-side validation logic.

If this key value is set to "None" [default], the ASP.NET application will use the pre-4.5 behavior (JavaScript inline in the pages) for client-side validation logic.

If this key value is set to "WebForms", ASP.NET uses HTML5 data-attributes and late bound JavaScript from an added script reference for client-side validation logic.

like image 153
Jaqen H'ghar Avatar answered Oct 12 '22 01:10

Jaqen H'ghar


To fix this problem on specific page need to set some validation settings when page loading. Write code below in Page_Load() method:

protected void Page_Load(object sender, EventArgs e)     {         ValidationSettings.UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;     } 

Its work for me in .NET 4.5

like image 29
Rostyslav Zhalivtsiv Avatar answered Oct 11 '22 23:10

Rostyslav Zhalivtsiv