You are in the “Web API with AJAX” article series. This series explains many topics directly or indirectly related to the Web API. You can visit our old articles here:
- Web API with AJAX: Understand POST request in Web API
- Web API with AJAX: Understand GET request in Web API
- Web API with AJAX: Make PUT Request in RESTful Web API Service
- Web API With AJAX: Understand DELETE Verb in Restful Web API
- Web API With AJAX: Use GetJSON() Function to Get JSON Data
- Web API with AJAX: Understand Method Name and Attribute in Web API
- Web API with AJAX: Understand FormBody and FormUri attribute inWeb API
- Web API With AJAX: Understand AcceptVerb Attribute in Web API
- Web API With AJAX: Various Parameters of jQuery Ajax() Function
- Web API with AJAX: Perform Cross-Domain AJAX Request using POST Verb
- Web API With AJAX: Cross Domain AJAX Call With GET Request
- Web API With AJAX: Return Various Data Types From Web API Service
- Web API With AJAX: Submit Form Data After Serialization
I hope that by seeing the title you have clicked on a link and your intention is to go through it very quickly or you may think, why a session management article? This series exlains the Web API in the perspective of AJAX. Yes you are correct; there is no direct relation between session management and AJAX. Intentionally I have put it here by believing the fact:
“Learning is learning, no matter from where it is”.
Ok, so now to start with our actual topic, session management in the Web API. We all know that the Web API is one kind of RESTful service built on top of the HTTP protocol. And by nature HTTP is stateless, in other words for a web server each and every request is a new request. But people are clever with the HTTP protocol (Ha..Ha..) they have introduced a trick to make the HTTP protocol a stateful protocol. The trick is nothing but a Session.
Now, we have all used a session in a normal WebForm application. And we have a basic concept of it but you may not have implemented a session in the Web API.
By default the session handling option is not enabled in the Web API. We need to configure it in the Global.asax page.
Use the following procedure to configure the entire application.
Step 1: Add the properties
Add the following two properties in the Global.asax page.
private const string _WebApiPrefix = "api";
private static string _WebApiExecutionPath = String.Format("~/{0}", _WebApiPrefix);
Step 2: Add the functions
Add both of the following functions within the “Application” class of the Global.asax page.
protected void Application_PostAuthorizeRequest()
{
if (IsWebApiRequest())
{
HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}
}
private static bool IsWebApiRequest()
{
return HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.StartsWith(_WebApiExecutionPath);
}
So here we are done with our Global.asax configuration. The following is the full implementation of the Global.asax page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.WebHost;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.SessionState;
namespace TestWEB_API
{
public class WebApiApplication : System.Web.HttpApplication
{
private const string _WebApiPrefix = "api";
private static string _WebApiExecutionPath = String.Format("~/{0}", _WebApiPrefix);


Sourabh DhimanPosted Jul 30, 2019, 6:49 AM
Hello SIr .How Handle Session in Web API with out MVC
Mukund ThakkarPosted Jul 31, 2016, 2:25 PM
The above code is not working in few condition when I call API method and Session is not available. I have added following code in Global file, but still it goes to my API Call instead of redirecting user to login page,protected void Session_Start() { if (Session["Username"] != null) { //Redirect to Welcome Page if Session is not null HttpContext.Current.Response.Redirect("~/WelcomeScreen", false); } else { //Redirect to Login Page if Session is null & Expires new RedirectToRouteResult(new RouteValueDictionary { { "action", "Index" }, { "controller", "Login" } }); } }
Ravi KPosted Apr 29, 2016, 9:24 AM
Hi Saurav, This does not work on production server when web and web api are hosted seperately.. can u help me ?
Joginder BangerPosted Aug 29, 2015, 7:36 AM
thanks Sourav Kayal , Dinesh Beniwal and teams start a great platefrom.. this articles save my lots of time.....