ASP.NET WebForms UnobtrusiveValidationMode jQuery映射错误已加配置仍无效求解决
Hey there, I’ve run into this exact issue before with ASP.NET WebForms, so let’s walk through some solutions that should get you past this error, even after setting ValidationSettings:UnobtrusiveValidationMode to None.
1. Double-check your web.config configuration placement
Sometimes the issue is simply that your appSettings entry is nested incorrectly in the web.config file. It needs to be a direct child of the root <configuration> node, not inside another section like <system.web> or elsewhere. Here’s the correct structure:
<configuration> <appSettings> <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /> </appSettings> <!-- Other configuration sections like system.web, connectionStrings, etc. --> </configuration>
If you had it nested somewhere else, moving it to the correct spot should resolve the error immediately.
2. Configure the ScriptResourceMapping for jQuery (if you want to keep Unobtrusive Validation enabled)
If you’d prefer not to disable Unobtrusive Validation, you need to properly map the jQuery resource so WebForms can find it. Add this code to your Global.asax.cs file inside the Application_Start method:
protected void Application_Start(object sender, EventArgs e) { // Register jQuery ScriptResourceMapping ScriptManager.ScriptResourceMappings.Add("jquery", new ScriptResourceDefinition { Path = "~/Scripts/jquery-3.6.4.min.js", // Update this to your jQuery file path/version DebugPath = "~/Scripts/jquery-3.6.4.js", CdnPath = "https://code.jquery.com/jquery-3.6.4.min.js", CdnDebugPath = "https://code.jquery.com/jquery-3.6.4.js" }); }
Make sure the Path points to the actual jQuery file in your project’s Scripts folder (adjust the version number to match what you have).
3. Verify jQuery is loaded in your pages/master page
Even if you have the right config, if jQuery isn’t actually being loaded on the page, the validation system will still throw this error. Ensure you have a reference to jQuery in your master page or individual WebForms pages, like this:
<script src="~/Scripts/jquery-3.6.4.min.js"></script>
Place this reference before any WebForms validation scripts or the ScriptManager control to make sure jQuery loads first.
4. Check for conflicting configuration settings
Occasionally, other validation-related settings in web.config can override your UnobtrusiveValidationMode setting. Look for entries like <pages validateRequest="true"> or other ValidationSettings keys in appSettings that might be conflicting. Remove or adjust those if they’re interfering.
Give these steps a try—most of the time, it’s either a misplaced config entry or a missing jQuery mapping/reference that’s causing the problem.
内容的提问来源于stack exchange,提问作者M.Carreira

