Session access disable for asp.net bundle requests

789 views Asked by At

I am using asp.net and MVC4 for my application and also i am using bundling for css and js files.

When i look into my trace files, i observed that all my bundle requests are using session. i.e, all bundle requests are passing through sessionstatemodule.

My trace looks like below.

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 

I think i dont need session access for my bundle requests. How can i disable the session access for my bundle requests?

1

There are 1 answers

0
Priyank Sheth On

If I understand your question correctly you want to disable session state for your static resources. For this you can do two things:

1) Disable SessionState for Controller

For that you need to import System.Web.SessionState namespace and you then decorate your controller with following line of code:

[SessioState(SessionStateBehavior.Disabled)
public class HomeController: Controller
{
}

For more information you can visit following link:

Controlling Session State Behavior

2) Create Static Resources

IIS Setup:

Create two website in your inetpub directory.

  1. www.domain.com // For main site
  2. static.domain.com // Foe static resources

Now point them to same physical directory i.e.

C:\inetpub\www.domain.com

Redirect domain.com to www.domain.com

Redirect any domain.com request to 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

** Code changes**

Add below code in your web.config file:

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

use PreApplicationStartMethod and Microsoft.Web.Infrastructure to dynamically register HTTP module in pre-application startup stage

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

    {

    }

}

Add an Extension Method

public static class Extensions

{

    public static string StaticContent(this UrlHelper url, string contentPath)

    {

        string strStaticDomain = ConfigurationManager.AppSettings["StaticDomain"];

        return contentPath.Replace("~", strStaticDomain);

    }

}

Now use @Url.StaticContent() from view so that it will render static resource url with static.domain.com whether it is image, script, CSS, or bundles or wherever we want to refer cookieless domain. for e.g.

<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="" />

Visit below link for full information as article is pretty big:

Cookie less domain for bundling and static resources

Hope this will help you to achieve your goal.