2016-05-14 72 views
11

一個ScriptResourceMapping在我的web應用程序,我得到以下錯誤:的WebForms UnobtrusiveValidationMode需要的jQuery

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).

我該如何解決呢?

+1

[ASP.Net 2012不唐突的驗證使用jQuery](的可能的複製http://stackoverflow.com/questions/12452109/asp-net-2012-unobtrusive-validation-with-jquery) – mihkov

+0

[WebForms UnobtrusiveValidationMode需要爲'jquery'提供ScriptResourceMapping。請添加ScriptResourceMapping命名的jQuery(區分大小寫)(https://stackoverflow.com/questions/16660900/webforms-unobtrusivevalidationmode-requires-a-scriptresourcemapping-for-jquery) –

回答

35

從.NET 4.5開始,Validators使用數據屬性和有界的Javascript來完成驗證工作,所以.NET期望您爲jQuery添加一個腳本引用。

有解決錯誤兩種可能的方式:


禁用UnobtrusiveValidationMode

一下添加到web.config中:

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

它會工作,因爲它的工作在以前的.NET版本中,只是將必要的Javascript添加到您的頁面以使驗證器正常工作,而不是尋找f或者你的jQuery文件中的代碼。實際上這是常見的解決方案。


另一種解決方案是註冊腳本:

在Global.asax中添加Application_Start映射到您的jQuery的文件路徑:從MSDN

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.min.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" 
    }); 
} 

一些細節:

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.

+1

由於其工作 –

+0

感謝很多很多人,你是天才 –

+1

頂級解決方案爲我解決了這個問題,在.Net 4.5.2 – Callat

0

我認爲這是針對此類型錯誤的最佳解決方案。 所以請在下面添加。 此外,它工作,我的代碼,當我使用MSVS 2015年

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

3

要解決特定的頁面在這個問題上需要設置一些驗證設置時,頁面加載。在Page_Load()方法如下編寫代碼:

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

它爲我工作在.NET 4.5

1

Jaqen H'ghar通過點上。第三種方法是:

  1. 轉到管理的NuGet軟件包
  2. 安裝Microsoft.jQuery.Unobtrusive.Validation
  3. 打開Global.asax中。CS文件並添加此代碼Application_Start方法中內部的在應用程序啓動運行

代碼:

ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition { 
    Path = "~/Scripts/jquery.validate.unobtrusive.min.js", 
    DebugPath = "~/Scripts/jquery.validate.unobtrusive.min.js" 
}); 
+0

這對我有用(.NET 4.6),但我正在閱讀一本書,我正在從「ASP.NET空網站「項目,它沒有Global.asax,所以我不得不在項目中添加一個」全局應用程序類「,該項目添加了Global.asax文件。 –