Asp.net mvc应用程序在用户登录之前抛出js语法错误

时间:2013-10-14 09:29:54

标签: c# javascript asp.net asp.net-mvc asp.net-mvc-4

在检查浏览器控制台时,为什么我的应用程序在登录页面上向我抛出了一些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

我做错了什么?

2 个答案:

答案 0 :(得分:3)

从我的ASP.Net体验(不是MVC):如果启用了表单身份验证,则必须绕过对登录页面中使用的所有脚本和映像的身份验证。这是通过使用web.config的location元素完成的,如下例所示:

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

答案 1 :(得分:0)

经过一些研发,我也面临着同样的问题,我发现我需要在Web配置中提供以下代码:

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