using System.Security.Principal; using System.Threading; using EmbedIO.Routing; using EmbedIO.Sessions; namespace EmbedIO.WebApi { /// /// Inherit from this class and define your own Web API methods /// You must RegisterController in the Web API Module to make it active. /// public abstract class WebApiController { // The HttpContext and Route properties are always initialized to non-null values, // but it's done after creation by a runtime-compiled lambda, // which the compiler cannot know about, hence the warnings. #pragma warning disable CS8618 // Non-nullable property is uninitialized. Consider declaring the property as nullable. /// /// Gets the HTTP context. /// This property is automatically initialized upon controller creation. /// public IHttpContext HttpContext { get; internal set; } /// /// Gets the resolved route. /// This property is automatically initialized upon controller creation. /// public RouteMatch Route { get; internal set; } #pragma warning restore CS8618 /// /// Gets the used to cancel processing of the request. /// public CancellationToken CancellationToken => HttpContext.CancellationToken; /// /// Gets the HTTP request. /// public IHttpRequest Request => HttpContext.Request; /// /// Gets the HTTP response object. /// public IHttpResponse Response => HttpContext.Response; /// /// Gets the user. /// public IPrincipal? User => HttpContext.User; /// /// Gets the session proxy associated with the HTTP context. /// public ISessionProxy Session => HttpContext.Session; /// /// This method is meant to be called internally by EmbedIO. /// Derived classes can override the method /// to perform common operations before any handler gets called. /// /// public void PreProcessRequest() => OnBeforeHandler(); /// /// Called before a handler to perform common operations. /// The default behavior is to set response headers /// in order to prevent caching of the response. /// protected virtual void OnBeforeHandler() => HttpContext.Response.DisableCaching(); } }