Asp.net mvc 登录页面上的Asp.NETMVC绑定

Asp.net mvc 登录页面上的Asp.NETMVC绑定,asp.net-mvc,authentication,bundle,Asp.net Mvc,Authentication,Bundle,我有一个Asp.NETMVC站点,它使用表单身份验证,没有“公共”访问权限。未经验证的请求重定向到我的登录控制器。在视图中,我通过bundle引用css和js文件。但是,部署时,对这些捆绑包的请求都会重定向到带有RedirectUrl参数的登录页面。有道理吗 那么,如何在不需要身份验证的情况下访问特定的bundle呢 作为一个糟糕的解决方法,我知道我可以只引用放置在公共文件夹中的单个文件,但这规避了所有最小化的好处 谢谢。您应该将这些捆绑包放在主视图页面上 在母版页上,您应该有如下内容: @Re

我有一个Asp.NETMVC站点,它使用表单身份验证,没有“公共”访问权限。未经验证的请求重定向到我的登录控制器。在视图中,我通过bundle引用css和js文件。但是,部署时,对这些捆绑包的请求都会重定向到带有RedirectUrl参数的登录页面。有道理吗

那么,如何在不需要身份验证的情况下访问特定的bundle呢

作为一个糟糕的解决方法,我知道我可以只引用放置在公共文件夹中的单个文件,但这规避了所有最小化的好处


谢谢。

您应该将这些捆绑包放在主视图页面上

在母版页上,您应该有如下内容:

@RenderSection("scripts", required: false)
@section Scripts{
//put all your scripts here
}
在您的视图中,只需添加如下脚本:

@RenderSection("scripts", required: false)
@section Scripts{
//put all your scripts here
}
您可以在标题中为所需的内容定义一个部分,并对css执行相同的操作

如果这不起作用,您可能需要确保捆绑包名称与站点中实际路径的名称不冲突,否则mvc引擎将处理请求,而不是提供文件

最糟糕的情况是,您必须启用对web.config中目录的匿名访问

<configuration>
<location path="content">
<system.web>
<authorization>
<allow users="?" />
</authorization>
</system.web>
</location>

<location path="scripts">
<system.web>
<authorization>
<allow users="?" />
</authorization>
 </system.web>
</location>

</configuration>
<location path="Content"> <!--or whatever you call your bundle path instead of Content-->
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
   </system.web>
</location>

您需要做几件事

首先,将要渲染的脚本和样式的名称更改为与应用程序中的文件夹不冲突的名称。因此,如果您有
~/Content/styles
文件夹,请将样式包命名为
~/Content/styles/css

捆绑包名称末尾的
/css
用于防止请求被视为脚本

其次,您需要为
内容
或您在web.config中引用的包路径添加授权

<configuration>
<location path="content">
<system.web>
<authorization>
<allow users="?" />
</authorization>
</system.web>
</location>

<location path="scripts">
<system.web>
<authorization>
<allow users="?" />
</authorization>
 </system.web>
</location>

</configuration>
<location path="Content"> <!--or whatever you call your bundle path instead of Content-->
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
   </system.web>
</location>


这将阻止表单身份验证重定向并提供您的内容。

谢谢。“真实”文件无需身份验证即可访问。例如,~/Content/style.css可以工作,但是@Styles.Render(“~/Content/style”)会遇到身份验证例程,并被重定向到Login?RedirectUrl=。。。这有意义吗?我可能有点误解,但你的回答似乎前后矛盾。您建议将
~/Content/styles/css
作为捆绑URL,但授权路径示例使用
捆绑
。为了清晰起见,编辑了我的示例。希望有帮助。