Nicklas Pouey-Winger
Nicklas Pouey-Winger

Reputation: 3023

Asp.net mvc application throwing js syntax errors before users are logged in

When inspecting the browser console, why is it that my application throws some javascript errors at me at the login page?

I've set my web.config to contain the following:

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

And then I have a _AnonymousUserLayout.cshtml that contains the following scripts:

<!-- 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>

And in my Login.cshtml (Rendered through RenderBody() in the layout-file) I'm including the following:

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

In my Global.asax.cs I have defined this filter:

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

Lastly, I'm redirected to the login page in my _ViewStart.cshtml:

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

}

Whenever I'm not logged in and redirected to the login page, I keep seeing the following errors in the developer console:

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

What am I doing wrong?

Upvotes: 3

Views: 1574

Answers (2)

Vishvadeep singh
Vishvadeep singh

Reputation: 1663

I am also facing the same problem after some R & D I have found that I need to give below code in web config:

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

Upvotes: 0

Tushar Sood
Tushar Sood

Reputation: 413

From my ASP.Net experience (not MVC): If Forms Authentication is enabled, you have to bypass the authentication for all the scripts and images that are being used in the login page. This is done by using the web.config's location element as per the below sample:

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

Upvotes: 3

Related Questions