Asp.net MVC 应用程序在用户登录之前引发 js 语法错误

本文关键字:js 错误 语法 登录 MVC net 应用程序 用户 Asp | 更新日期: 2023-09-27 18:35:10

检查浏览器控制台时,为什么我的应用程序在登录页面向我抛出一些javascript错误?

我已经将我的 web.config 设置为包含以下内容:

<authentication mode="Forms">
  <forms loginUrl="~/Login" timeout="90" />
</authentication>

然后我有一个包含以下脚本的_AnonymousUserLayout.cshtml

<!-- Bootstrap JS -->
<script src="@Url.Content("~/Scripts/jquery-1.8.2.js")"></script>
<script src="@Url.Content("~/Scripts/bootstrap.js")"></script>
<!-- Supersized JS -->
<script src="@Url.Content("~/Scripts/supersized.core.3.2.1.js")"></script>

在我的Login.cshtml(通过布局文件中的 RenderBody() 渲染)中,我包括以下内容:

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

在我的Global.asax.cs中,我定义了这个过滤器:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new System.Web.Mvc.AuthorizeAttribute());
}

最后,我被重定向到_ViewStart.cshtml中的登录页面:

@{
    if (Request.IsAuthenticated)
    {
        Layout = "~/Views/Shared/_Layout.cshtml";    
    }
    else
    {
        Layout = "~/Views/Shared/_AnonymousUserLayout.cshtml";
    }
}

每当我没有登录并重定向到登录页面时,我都会在开发人员控制台中看到以下错误:

Uncaught SyntaxError: Unexpected token < :54837/Login?ReturnUrl=%2FScripts%2Fkendo%2F2013.1.319%2Fjquery.min.js:1
Uncaught SyntaxError: Unexpected token < :54837/Login?ReturnUrl=%2FScripts%2Fkendo%2F2013.1.319%2Fkendo.all.min.js:1
Uncaught SyntaxError: Unexpected token < Login?ReturnUrl=%2FScripts%2Fkendo%2F2013.1.319%2Fkendo.aspnetmvc.min.js:1

我做错了什么?

Asp.net MVC 应用程序在用户登录之前引发 js 语法错误

根据我 ASP.Net 的经验(不是 MVC):如果启用了表单身份验证,则必须绕过登录页中使用的所有脚本和图像的身份验证。 这是通过使用 web.config 的位置元素完成的,如以下示例所示:

<location path="<your path>jquery-1.8.2.js">
<system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
</system.web>

经过

一些研发,我也面临着同样的问题,我发现我需要在 web config 中给出以下代码:

<location path="Scripts/jquery-3.4.1.js">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>