我正在为我的应用程序使用 asp.net 和 MVC4,而且我正在为 css 和 js 文件使用捆绑。
当我查看我的跟踪文件时,我发现我所有的捆绑请求都在使用 session 。即,所有包请求都通过 sessionstatemodule。
我的轨迹如下所示。
155. NOTIFY_MODULE_START ModuleName="Session", Notification="REQUEST_ACQUIRE_STATE", fIsPostNotification="false" 06:59:43.480
156. AspNetPipelineEnter Data1="System.Web.SessionState.SessionStateModule" 06:59:43.480
157. AspNetSessionDataBegin 06:59:43.480
158. AspNetSessionDataEnd 06:59:43.996
159. AspNetPipelineLeave Data1="System.Web.SessionState.SessionStateModule" 06:59:43.996
160. NOTIFY_MODULE_COMPLETION ModuleName="Session", Notification="REQUEST_ACQUIRE_STATE", fIsPostNotificationEvent="false", CompletionBytes="0", ErrorCode="The operation completed successfully. (0x0)" 06:59:43.996
161. NOTIFY_MODULE_END ModuleName="Session", Notification="REQUEST_ACQUIRE_STATE", fIsPostNotificationEvent="false", NotificationStatus="NOTIFICATION_CONTINUE" 06:59:43.996
我认为我的 bundle 请求不需要 session 访问权限。如何为我的 bundle 请求禁用 session 访问?
最佳答案
如果我正确理解你的问题,你想为你的静态资源禁用 session 状态。为此,您可以做两件事:
1) 为Controller
禁用SessionState
为此,您需要导入 System.Web.SessionState
命名空间,然后使用以下代码行装饰您的 Controller :
[SessioState(SessionStateBehavior.Disabled)
public class HomeController: Controller
{
}
有关更多信息,您可以访问以下链接:
Controlling Session State Behavior
2) 创建静态资源
IIS 设置:
在您的 inetpub 目录中创建两个网站。
- www.domain.com//对于主站点
- static.domain.com//静态资源
现在将它们指向相同的物理目录即
C:\inetpub\www.domain.com
将 domain.com 重定向到 www.domain.com
将任何 domain.com 请求重定向到 www.domain.com。
so that any domain.com request must be redirected to www.domain.com because cookie set for domain.com will also be shared by all sub domain including static.domain.com hence it is much important steps
** 代码更改**
在您的 web.config 文件中添加以下代码:
<appSettings>
<add key="StaticSiteName" value="static.domain.com"/>
<add key="StaticDomain" value="http://static.domain.com"/>
<add key="MainDomain" value="http://www.domain.com"/>
</appSettings>
在预应用启动阶段使用PreApplicationStartMethod
和Microsoft.Web.Infrastructure
动态注册HTTP模块
public class PreApplicationStart
{
public static void Start()
{
string strStaticSiteName = ConfigurationManager.AppSettings["StaticSiteName"];
string strCurrentSiteName = HostingEnvironment.SiteName;
if (strCurrentSiteName.ToLower() == strStaticSiteName.ToLower())
{
DynamicModuleUtility.RegisterModule(typeof(StaticResource));
}
}
}
public class StaticResource : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
string strUrl = context.Request.Url.OriginalString.ToLower();
//HERE WE CAN CHECK IF REQUESTED URL IS FOR STATIC RESOURCE OR NOT
if (strUrl.Contains("Path/To/Static-Bundle/Resource") == false)
{
string strMainDomain = ConfigurationManager.AppSettings["MainDomain"];
context.Response.Redirect(strMainDomain);
}
}
public void Dispose()
{
}
添加扩展方法
public static class Extensions
{
public static string StaticContent(this UrlHelper url, string contentPath)
{
string strStaticDomain = ConfigurationManager.AppSettings["StaticDomain"];
return contentPath.Replace("~", strStaticDomain);
}
}
现在在 View 中使用 @Url.StaticContent()
以便它会呈现带有 static.domain.com 的静态资源 url,无论它是图像、脚本、CSS 还是 bundle 或者我们想要的任何地方引用 cookieless 域。例如
<link href="@Url.StaticContent("~/Content/Site.css")" rel="Stylesheet" />
<script src="@Url.StaticContent("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>
<script src="@Url.StaticContent("~/bundles/jquery")" type="text/javascript"></script>
<img src="@Url.StaticContent("~/Images/heroAccent.png")" alt="" />
由于文章很大,请访问下面的链接以获取完整信息:
Cookie less domain for bundling and static resources
希望这能帮助您实现目标。
关于asp.net - 对 asp.net 包请求禁用 session 访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30998903/