- Web API本身是默认没有开启Session配置的
- 建立SessionRouteHandler.cs和SessionControllerRouteHandler.cs并覆写类HttpControllerHandler、IRequiresSessionState、HttpControllerRouteHandler
1 public class SessionRouteHandler: HttpControllerHandler, IRequiresSessionState{
2 public SessionRouteHandler(RouteData routeData) : base(routeData){ }
3 }
4 public class SessionControllerRouteHandler: HttpControllerRouteHandler{
5 protected override IHttpHandler GetHttpHandler(RequestContext requestContext){
6 return new SessionRouteHandler(requestContext.RouteData);
7 }
8 }
- 在WebApiConfig.cs中将config.Routes.MapHttpRoute 改成RouteTable.Routes.MapHttpRoute(using System.Web.Routing)并指定RouteHandler
1 RouteTable.Routes.MapHttpRoute( 2 name: "ActionApi", 3 routeTemplate: "actionapi/{controller}/{action}/{id}", 4 defaults: new { id = RouteParameter.Optional }).RouteHandler = new SessionControllerRouteHandler();