ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Set Session in Web API using jQuery Ajax in ASP.Net<p>Hi aspsun11,</p> <p>Add&nbsp;Application_PostAuthorizeRequest in&nbsp;global.asax to access Session in Web API controller.</p> <p>Check the below example.</p> <p><span style="text-decoration: underline;"><strong>global.asax</strong></span></p> <pre class="brush: csharp">public class Global : HttpApplication { void Application_Start(object sender, EventArgs e) { // Code that runs on application startup GlobalConfiguration.Configure(WebApiConfig.Register); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } protected void Application_PostAuthorizeRequest() { HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required); } }</pre> <p><strong><span style="text-decoration: underline;">API Controller</span></strong></p> <pre class="brush: csharp">public class SessionController : ApiController { [HttpPost] public string SetSession() { System.Web.HttpContext.Current.Session[&#34;Time&#34;] = DateTime.Now; return System.Web.HttpContext.Current.Session[&#34;Time&#34;].ToString(); } }</pre> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;script src=&#34;Scripts/jquery-1.10.2.js&#34;&gt;&lt;/script&gt; &lt;script&gt; $(document).ready(function () { $(&#34;#Save&#34;).click(function () { $.ajax({ url: &#39;http://localhost:3117/api/Session/SetSession&#39;, type: &#39;POST&#39;, dataType: &#39;json&#39;, data: {}, success: function (data) { document.write(data); }, error: function (xhr) { } }); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;input type=&#34;button&#34; id=&#34;Save&#34; value=&#34;Save&#34; /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><strong><span style="text-decoration: underline;">Output</span></strong></p> <p>09-08-2019 00:19:20</p>https://www.aspforums.net:443/Threads/125549/Set-Session-in-Web-API-using-jQuery-Ajax-in-ASPNet/https://www.aspforums.net:443/Threads/125549/Set-Session-in-Web-API-using-jQuery-Ajax-in-ASPNet/Thu, 08 Aug 2019 12:49:38 GMT