WebForms UnobtrusiveValidationMode需要jquery的ScriptResourceMapping [英] WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

查看:210
本文介绍了WebForms UnobtrusiveValidationMode需要jquery的ScriptResourceMapping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web应用程序中,出现以下错误:

In my web application I get the following error:


WebForms UnobtrusiveValidationMode需要ScriptResourceMapping
来作为 jquery。请添加一个名为
jquery(区分大小写)的ScriptResourceMapping。

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

说明:在执行当前Web请求
时发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多
信息。

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.

异常详细信息:System.InvalidOperationException:WebForms
UnobtrusiveValidationMode需要针对
jquery的ScriptResourceMapping。请添加一个名为
jquery(区分大小写)的ScriptResourceMapping。

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

我该如何解决?

推荐答案

.NET 4.5以来,验证器使用数据属性和有限的Javascript进行验证工作,因此.NET希望您为以下内容添加脚本参考: jQuery。

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.

有两种方法可以解决该错误:

禁用 UnobtrusiveValidationMode

将此添加到web.config:

Add this to web.config:

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

它将像以前的.NET版本一样工作,并且只会在页面中添加必要的Javascript使验证器起作用,而不是在jQuery文件中查找代码。实际上,这是常见的解决方案。

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:

在Global.asax Application_Start 中将映射添加到jQuery文件路径:

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






来自MSDN的一些详细信息:


Some details from MSDN:


ValidationSettings:UnobtrusiveValidationMode 指定ASP.NET
如何全局启用内置的验证程序控件使用不引人注目的
JavaScript进行客户端验证逻辑。

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

如果此键值设置为 None(默认),则ASP.NET应用程序
将使用4.5之前的行为(页面中的JavaScript内联)进行
客户端验证逻辑。

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.

如果此键值设置为 WebForms,ASP.NET使用HTML5数据属性和来自添加的脚本参考的后期绑定JavaScript进行客户端验证逻辑。

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.

这篇关于WebForms UnobtrusiveValidationMode需要jquery的ScriptResourceMapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆