C# WebApi的controller中如何存取session
在MVC以后,Session方式可能已经不太常用,但偶尔还是会用到,比如页面验证码之类的。例如登录页面使用的验证码通过Controller提供一个View来实现,可以使用Session来存储这个值。但现在常用的请求都可以交给WebApi来处理,按照默认方式架设的MVC4的应用中,WebApi是取不到由Controller保存的Session值的,那么,我们需要进行一定的配置。记录如下:
第一步:简单建立两个类,以备修改WebApi的路由方式
第一个类SessionRouteHandler,继承自HttpControllerHandler,并同时实现一下IRequiresSessionState接口,其实IRequiresSessionState是没有内部方法的,因此也不需要写啥。
1 public class SessionRouteHandler : HttpControllerHandler,IRequiresSessionState
2 {
3 public SessionRouteHandler(RouteData routeData) : base(routeData)
4 {
5 }
6 }
第二个类SessionControllerRouteHandler,继承自HttpControllerRouteHandler
1 public class SessionControllerRouteHandler : HttpControllerRouteHandler
2 {
3 protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
4 {
5 return new SessionRouteHandler(requestContext.RouteData);
6 }
7 }
完成这两个类之后,就可以进行下一步了。
第二步:修改WebApiConfig,给新手提示一下,这个类在Global中可以看到,WebApiConfig.Register(......这里在进行的。一般情况下,在App_Start目录下。由于我们是要让WebApi能获得MVC中Controller中设置的Session,故要重改一下这个配置。
1 public static class WebApiConfig
2 {
3 public static void Register(HttpConfiguration config)
4 {
5 //config.Routes.MapHttpRoute(
6 // name: "DefaultApi",
7 // routeTemplate: "api/{controller}/{id}",
8 // defaults: new { id = RouteParameter.Optional }
9 //);
10 //Route上传递Session
11 RouteTable.Routes.MapHttpRoute(
12 name: "DefaultApi",
13 routeTemplate: "api/{controller}/{id}",
14 defaults: new {id = RouteParameter.Optional}).RouteHandler = new SessionControllerRouteHandler();
15 }
16 }
这里从RouteTable直接配置,并指定了RouteHandler为我们第一步建立的SessionControllerRouteHandler。但这些工作都做了,Session的传递还是会不成功的,因为这里我们要指定一下Behavior。
第三步:修改Global。
在Global.asax.cs代码中,重载它的Init方法,加入如下代码:
1 public override void Init()
2 {
3 PostAuthenticateRequest += (s, e) => HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
4 base.Init();
5 }
好了,全部完成后可以编译了,这时候,你可以在Controller中按照正常方式建立Session了,例如:
Session["ValidCode"]="Session Test"
那么WebApi中就可以获取到了:
HttpContext.Current.Session["ValidCode"].ToString()
在MVC以后,Session方式可能已经不太常用,但偶尔还是会用到,比如页面验证码之类的。例如登录页面使用的验证码通过Controller提供一个View来实现,可以使用Session来存储这个值。但现在常用的请求都可以交给WebApi来处理,按照默认方式架设的MVC4的应用中,WebApi是取不到由Controller保存的Session值的,那么,我们需要进行一定的配置。记录如下:
第一步:简单建立两个类,以备修改WebApi的路由方式
第一个类SessionRouteHandler,继承自HttpControllerHandler,并同时实现一下IRequiresSessionState接口,其实IRequiresSessionState是没有内部方法的,因此也不需要写啥。
1 public class SessionRouteHandler : HttpControllerHandler,IRequiresSessionState
2 {
3 public SessionRouteHandler(RouteData routeData) : base(routeData)
4 {
5 }
6 }
第二个类SessionControllerRouteHandler,继承自HttpControllerRouteHandler
1 public class SessionControllerRouteHandler : HttpControllerRouteHandler
2 {
3 protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
4 {
5 return new SessionRouteHandler(requestContext.RouteData);
6 }
7 }
完成这两个类之后,就可以进行下一步了。
第二步:修改WebApiConfig,给新手提示一下,这个类在Global中可以看到,WebApiConfig.Register(......这里在进行的。一般情况下,在App_Start目录下。由于我们是要让WebApi能获得MVC中Controller中设置的Session,故要重改一下这个配置。
1 public static class WebApiConfig
2 {
3 public static void Register(HttpConfiguration config)
4 {
5 //config.Routes.MapHttpRoute(
6 // name: "DefaultApi",
7 // routeTemplate: "api/{controller}/{id}",
8 // defaults: new { id = RouteParameter.Optional }
9 //);
10 //Route上传递Session
11 RouteTable.Routes.MapHttpRoute(
12 name: "DefaultApi",
13 routeTemplate: "api/{controller}/{id}",
14 defaults: new {id = RouteParameter.Optional}).RouteHandler = new SessionControllerRouteHandler();
15 }
16 }
这里从RouteTable直接配置,并指定了RouteHandler为我们第一步建立的SessionControllerRouteHandler。但这些工作都做了,Session的传递还是会不成功的,因为这里我们要指定一下Behavior。
第三步:修改Global。
在Global.asax.cs代码中,重载它的Init方法,加入如下代码:
1 public override void Init()
2 {
3 PostAuthenticateRequest += (s, e) => HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
4 base.Init();
5 }
好了,全部完成后可以编译了,这时候,你可以在Controller中按照正常方式建立Session了,例如:
Session["ValidCode"]="Session Test"
那么WebApi中就可以获取到了:
HttpContext.Current.Session["ValidCode"].ToString()