mvc webapi+autofac + session 的使用

  先说说我的项目情况:MVC5+AUTOFAC,下面就直接说说怎么加入webapi、autofac的配置、登录使用session

一、MVC5添加WEBAPI

1、添加

参考文章:https://blog.csdn.net/qwlovedzm/article/details/56003770

1. 增加一个WebApi Controller, VS 会自动添加相关的引用,主要有System.Web.Http,System.Web.Http.WebHost,System.Net.Http,并在App_Start 下创建 WebApiConfig.cs 并注册路由

2.在Global.asax, Application_Start 下添加 WebAPI 配置,添加:GlobalConfiguration.Configure(WebApiConfig.Register);

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);//增加此行
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

 2.基本使用

   webapi是完全基于RESTful标准的,只有几种标准的请求,如get,post,put,delete等。

  和mvc的用法不一样, 对于新手来说还是有一些坑的,

   常见错误: 无法将多个参数( )绑定到请求的内容

  相关文章推荐:

  https://www.cnblogs.com/babycool/p/3922738.html

  https://www.itsvse.com/thread-3044-1-1.html

二、为WEBAPI配置autofac

  参考文章:https://www.cnblogs.com/xujie520/p/5200815.html

   1、使用nupkg引用Autofac,Autofac.Mvc5和Autofac.Webapi2 

  2.注册组件.

var builder=new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());//注册api容器的实现
builder.RegisterControllers(Assembly.GetExecutingAssembly());//注册mvc容器的实现
......
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container.Container);

   

三、WEBAPI下使用session

   原文:http://www.cnblogs.com/CreateMyself/p/5178747.html

  在Web APi中是不支持Session的,说的更加精准一点是默认没有启动Session。

  在Web APi中的Session不依赖于System.Web,如若要将Web APi运行在ASP.NET运行时,此时需要启动Session,在Web APi环境中就启动APi该如何做?

  有两种实现方式:访问路径判断、自定义RouteHandle

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值