c# - 当用户(尚未)获得授权时,ASP.NET(3.5,Webforms)无法正确加载 CSS

标签 c# css asp.net webforms

我这里有一个非常奇怪的问题...我继承了一个相当大的 ASP.NET 3.5 Webforms 元素,一个问题是当启动应用程序时,会显示登录页面 - 但没有任何样式。 ...

这是 Login.aspx 标记(已简化):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" 
         Inherits="MyWeb.Web.Login" MasterPageFile="~/MyWebPublic.Master" 
         UICulture="Auto" Culture="Auto" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
   ... some div's, textboxes etc.
</asp:Content>   

此页面基于“公共(public)”母版页 - 如下所示:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MyWebPublic.Master.cs" Inherits="MyWeb.MyWebPublic" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <link rel="stylesheet" type="text/css" href="styles/style.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="styles/layout.css" media="screen" />
</head>
<body>
  ...
</body>

因此您可以看到 - 我的母版页中确实有指向两个 CSS 样式表的链接,它们实际上存在。

我的 web.config,我特别允许所有人(甚至是非授权用户)访问我的登录页面:

<!-- Allow access to login page for everyone -->
<location path="Login.aspx">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
</location>

然而 - 当我运行该元素时,我得到一个没有应用任何 CSS 的丑陋页面。在 Firefox 错误控制台中,我发现:

The stylesheet http://localhost:50855/Login.aspx?ReturnUrl=%2fstyles%2flayout.css was not loaded because its MIME type, "text/html", is not "text/css". Login.aspx
The stylesheet http://localhost:50855/Login.aspx?ReturnUrl=%2fstyles%2fstyle.css was not loaded because its MIME type, "text/html", is not "text/css".

所以看起来好像 ASP.NET 正在尝试加载 CSS,确定这是一个未经授权的用户,并重定向到登录页面 - 即使在尝试加载 CSS 时也是如此!

登录后 - 一切正常,我的 CSS 已按预期应用...

这里到底发生了什么??我以前从未见过这种行为 - 在所有其他情况下,即使是您到达的第一页(登录或其他方式)也会通过应用引用的 CSS 获得其正确的样式......

有什么想法、想法吗?

最佳答案

您授权任何用户访问登录页面,但不能访问样式目录中的内容。由于 css 是通过 ASP.NET 提供的,因此在用户通过身份验证之前不允许访问。将以下内容添加到现有位置元素下的 web.config。

<location path="styles">
    <system.web>
        <authorization>
            <allow users="*" />
        </authorization>
    </system.web>
</location>

另请检查此链接以获取可能的解决方案:https://stackoverflow.com/a/804061/1444207

关于c# - 当用户(尚未)获得授权时,ASP.NET(3.5,Webforms)无法正确加载 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25301892/

相关文章:

html - 响应式标题图片的任何其他技术

html - css 卡片翻转 Internet Explorer

css - 如何一次显示 2 个渐变?

asp.net - 将 JSON 传递给需要 List<T> 参数的 PageMethod

c# - 从 url 中检索电子邮件 ID

c# - 在滚动文件的顶部仅包含一次标题

c# - 将视频渲染到帧速率可能不一致的文件

c# - 应用程序在 15 分钟后崩溃,WCE 6.0 CF 3.5 Motorola MC3190

c# - 如何在 C# 中使用数据读取器循环遍历行?

c# - WPF MVVM 中的 NHibernate session