1

I have some logic in Session_Start and this logic is actual for all my controller methods except one special method. I need to not execute Session_start logic, when user goes to special method URL.

Any ideas how I can do that?

halfer
  • 19,824
  • 17
  • 99
  • 186
Frank59
  • 3,141
  • 4
  • 31
  • 53
  • I am not if I understand you correctly, you don't want the Session_Start method in in e.g. your global.asax to be executed when a special controller/url is invoked? I don't think this is possible, since Session_Start will be invoked by the first Request of a "new" client (e.g. without an existing session). It also may depend on your session settings/ mode (cookieless,...) – Bernhard Kircher Sep 06 '13 at 10:55
  • Yes, i want to ignore Session_Start logic, when special controller method is invoked? If i cant avoid raise Session_start on this url, may be i can detect url from request and make 'return' before main Session_Start logic – Frank59 Sep 06 '13 at 11:00
  • Maybe the following links can help you: [Session State Oveview](http://msdn.microsoft.com/en-us/library/ms178581(VS.100).aspx) [Lifecycle](http://technet.microsoft.com/library/Cc960840) – Bernhard Kircher Sep 06 '13 at 11:02

1 Answers1

1

As far as I understand your question, you do not want the code within your Session_Start method to be invoked if a special url is requested. I think it would be helpful to know what your problem is, that you want to solve. For now here is my answer:

Since Session_Start is only invoked once (at least usually, depending on your configuration of the session module - see my comments to your question), this only works if the client invokes the "special" url first, e.g. before calling other urls. If another url has been invoked first, session will be initialized according to your code. Important: as mentioned, depending on your configuration, there will be always a Session (but in this special case you do not want to execute your custom logic in Session_Start):

You can use the Current HttpRequest and perform a check on some properties:

// this will (usually) only be called once, on the first request of the client
protected void Session_Start() {
        // perform your check here if this is the url you want to exclude
        if (HttpContext.Current.Request.Url.OriginalString.ToLowerInvariant().EndsWith("something")) {
            return;
        }

        // your initialization here that should not be executed for clients accessing the site using the above url
    }

As you can see, you can access the Request object, and perform your check there, depending on your requriements.

Bernhard Kircher
  • 4,132
  • 3
  • 32
  • 38
  • Thanks for answer. I thinked about that solution, but it need some logic for hack protection. If i not find more dependable solution i will use that way. – Frank59 Sep 06 '13 at 11:34
  • hack protection? if you have a security problem ore something important, my answer is defenitely not a solution. In order to help, I'd need more details... – Bernhard Kircher Sep 06 '13 at 11:37