UnobtrusiveValidationMode jquery w VS for Web

Chciałem dodać, możliwość logowania się do mojej testowej strony, jednakże za każdym razem wskakiwał mi komunikat o błędzie UnobtrusiveValidationMode jquery. Aplikacja zwracała błąd iż wymagany jest „ScriptResourceMapping for ‚jquery’”.

UWAGA! Ten post został zamieszczony na mojej starej stronie, która dawno temu padła. Archiwizuje stare artykuły których nie zweryfikowałem, może kiedyś się przydadzą – proszę z rezerwą podchodzić do treści tu zwartych.

Cały komunikat o błędzie:

Server Error in '/' Application.
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).

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidOperationException: WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).]
   System.Web.UI.ClientScriptManager.EnsureJqueryRegistered() +2179162
   System.Web.UI.WebControls.BaseValidator.RegisterUnobtrusiveScript() +10
   System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +9708897
   System.Web.UI.Control.PreRenderRecursiveInternal() +83
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408

Trochę poszperałem i znalazłem rozwiązanie, aby wyłączyć walidację dodając ten kod do źródła pliku Web.config tuż za fragmentem <configuration>

Rozwiązanie I: Modyfikacja pliku Web.config

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

A żeby dodać jquery do naszego projektu trzeba w konsoli wpisać takie polecenie (tego nie jestem pewien):

PM> Install-Package jQuery

Rozwiązanie II: Modyfikacja pliku Global.asax

Źródło: msdn.microsoft.com

W projekcie dodajemy plik Global.asax. Później w pliku w fragmencie Application_Start dodajemy następujący kod:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
‚ Code that runs on application startup
Dim myScriptResDef As New ScriptResourceDefinition()
myScriptResDef.Path = „~/Scripts/jquery-1.4.2.min.js”
myScriptResDef.DebugPath = „~/Scripts/jquery-1.4.2.js”
myScriptResDef.CdnPath = „http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js”
myScriptResDef.CdnDebugPath = „http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.js”
ScriptManager.ScriptResourceMapping.AddDefinition(„jquery”, Nothing, myScriptResDef)

End Sub

Microsoft informuje, że należy dodać taki kod na stronie przed każdym elementem który używa skryptu jQuery.

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *

Witryna wykorzystuje Akismet, aby ograniczyć spam. Dowiedz się więcej jak przetwarzane są dane komentarzy.