1. 在 Global.asax.cs 文件中加入session支持

        protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
public override void Init()
{
//注册事件
this.AuthenticateRequest += WebApiApplication_AuthenticateRequest;
base.Init();
}
void WebApiApplication_AuthenticateRequest(object sender, EventArgs e)
{
//启用 webapi 支持session 会话
HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
}

 2.在 WebApiConfig.cs 中设置 路由session 传递

       public static void Register(HttpConfiguration config)
{ RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
).RouteHandler = new SessionControllerRouteHandler(); }
public class SessionRouteHandler : HttpControllerHandler, IRequiresSessionState
{
public SessionRouteHandler(RouteData routeData)
: base(routeData)
{
}
}
public class SessionControllerRouteHandler : HttpControllerRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new SessionRouteHandler(requestContext.RouteData);
}
}

3. 在web,config 中设置跨域配值

    <httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Credentials" value="true" />
//设置 前端ip地址
<add name="Access-Control-Allow-Origin" value="http://localhost:8080" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>

WebApi 中使用 Session的更多相关文章

  1. .net webapi 中使用session是出错 HttpContext.Current.Session==null

    最近在写.net webapi时发现 HttpContext.Current.Session==null  ,导致报错,后来查资料发现webapi中使用session时首先需要开启session功能, ...

  2. .NET WebAPI中使用Session使用

    问题及其解决方案: 今天做项目的时候因为需要编写一个短信验证码的接口我需要在我的后台.net webapi中存入我随机生成的短信验证码方便与前端传递过来的数据对比,所以决定使用session做缓存.但 ...

  3. WebApi中使用session

    webapi默认是不支持session的,要通过一些手动配置来开启Session功能 在Global.asax里添加: 导入命名空间: using System.Web.SessionState; p ...

  4. WebApi中的Session与Token间的处理对接

    首先,说起来创建session,一般会针对注册登录或者授权等情况: session 从字面上讲,就是会话.这个就类似于你和一个人交谈,你怎么知道当前和你交谈的是张三而不是李四呢?对方肯定有某种特征(长 ...

  5. C# WebApi 中设置Session可用

    在Global.acax中,添加下面方法 //设置session可用 public override void Init() { PostAuthenticateRequest += MvcAppli ...

  6. WebAPI中无法获取Session对象的解决办法

    在MVC的WebApi中默认是没有开启Session会话支持的.需要在Global中重写Init方法来指定会话需要支持的类型 public override void Init() { PostAut ...

  7. .net webapi项目中支持session

    webapi中默认是不支持session的开启的 需要在Global.asax文件中,添加如下代码 public override void Init() { this.PostAuthenticat ...

  8. webapi中session为null的解决方案

    Session  webapi中session为null的解决方案 在Global.asax里添加:开启Session功能(默认是不开启) 重写init方法 public class WebApiAp ...

  9. Asp.Net WebAPI中Filter过滤器的使用以及执行顺序

    转发自:http://www.cnblogs.com/UliiAn/p/5402146.html 在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行 ...

随机推荐

  1. x-pack破解并安装

    声明:本文仅作为学习交流,请勿用于商业用途,否则后果自负.如需使用黄金或白金版X-Pack请购买正版. 1. 安装x-pack 具体安装过程参照:http://www.cnblogs.com/shao ...

  2. 【Hive】Hive 安装&使用基础

    2 安装 2.1 参考 2.1.1 下载 2.1.1.1 https://mirrors.tuna.tsinghua.edu.cn/apache/hive/stable-2/ 2.1.2 安装指导 2 ...

  3. [转] docker rmi命令-删除image

    原文:http://www.simapple.com/341.html ---------------------------------------------------------------- ...

  4. TCP/IP详解 卷一(第十三章 IGMP:Internet组管理协议)

    本章将介绍用于支持主机和路由器进行多播的Internet组管理协议(IGMP) 它让一个物理网络上的所有系统知道主机当前所在的多播组.多播路由器需要这些信息以便知道多播数据报应该向那些接口转发. 跟I ...

  5. 深入理解dp px density

    1 http://blog.csdn.net/lcaihy1314/article/details/8446401 2 待续

  6. Git常用统计命令

    上周要做个汇报PPT涉及到个人对项目贡献量,在网上搜集了些常用统计命令,总结如下: 1.统计代码提交量(包括添加.删除): git log --author="$(gitconfig--ge ...

  7. SELECT * INTO xx FROM x0

    insert into a select * from b:--向存在表中插入数据,如果不存在表a报错. select * into a from b:--创建新表的同时插入数据,如果表a存在,报错. ...

  8. sql CHARINDEX() 与 PATINDEX() LEN() substring() COLLATE RAISERROR

    CHARINDEX()  在一个表达式中搜索另一个表达式并返回其起始位置(如果找到). CHARINDEX ( expressionToFind , expressionToSearch [ , st ...

  9. shader之旅-7-平面阴影(planar shadow)

    根据<real-time shadow>这本书第二章中的推导,实现了最简单的阴影技术. planar shadow通过一个投影矩阵将被灯光照射的物体的顶点沿着光线方向投影到接受阴影的平面. ...

  10. asp.net core mvc视频A:笔记2-4.ActionResult(动作结果,即返回值)

    json类型测试 方法一:实例化对象方式 代码 运行结果 方法二:封装方式 代码改动 运行结果 重点视图返回介绍,其他的不做介绍了 项目文件目录及文件添加 代码 运行结果 如果要显示的不是默认视图,可 ...