Last active
June 1, 2016 17:34
-
-
Save seangwright/7c735ef88fb29a6b11786565ab653296 to your computer and use it in GitHub Desktop.
Startup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Owin; | |
using Microsoft.Owin.Extensions; | |
using Owin; | |
using System.Web; | |
using System.Web.Http; | |
using System.Web.SessionState; | |
using WiredViews.WIR03.Web.Api.Configuration; | |
[assembly: OwinStartup(typeof(WiredViews.WIR03.Web.Api.Startup))] | |
namespace WiredViews.WIR03.Web.Api | |
{ | |
/// <summary> | |
/// Handles all application initialization for Web Api based routes | |
/// </summary> | |
public partial class Startup | |
{ | |
/// <summary> | |
/// This is the base of all api routes in this application | |
/// </summary> | |
private const string BaseApiRoute = "api"; | |
/// <summary> | |
/// Entrypoint for our web api application as a convention of the framework | |
/// </summary> | |
/// <param name="appBuilder"></param> | |
public void Configuration(IAppBuilder appBuilder) | |
{ | |
// Required to integrate Web Api requests with Kentico's reliance on Session State | |
RequireAspNetSession(appBuilder); | |
HttpConfiguration config = new HttpConfiguration(); | |
InitializeWebApiConfiguration(appBuilder, config); | |
} | |
/// <summary> | |
/// Handles setting up the Web API 2 portion of the application | |
/// </summary> | |
/// <param name="app"></param> | |
/// <param name="config"></param> | |
public void InitializeWebApiConfiguration(IAppBuilder app, HttpConfiguration config) | |
{ | |
SwashbuckleConfiguration.Configure(config); | |
WebApiConfig.Register(config, BaseApiRoute); | |
app.UseWebApi(config); | |
} | |
/// <summary> | |
/// Sets the Session State behavior as 'ReadOnly' for the given application pipeline (Web Api) | |
/// </summary> | |
/// <param name="app"></param> | |
public static void RequireAspNetSession(IAppBuilder app) | |
{ | |
app.Use((context, next) => | |
{ | |
if (context.Request.Path.Value.StartsWith($"/{BaseApiRoute}")) | |
{ | |
var httpContext = context.Get<HttpContextBase>(typeof(HttpContextBase).FullName); | |
httpContext.SetSessionStateBehavior(SessionStateBehavior.ReadOnly); | |
} | |
return next(); | |
}); | |
// To make sure the above `Use` is in the correct position: | |
app.UseStageMarker(PipelineStage.MapHandler); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment