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

猜你喜欢

转载自www.cnblogs.com/xcsn/p/9340683.html
今日推荐