• 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

  • 相关阅读:
    Raspberry PI B+ debian + wifi 网络设置
    数据库表结构对比同步mysqldiff
    Wysiwyg Editors 标签过滤
    常用MySQL语句
    解决"is marked as crashed and should be repaired"方法
    Selinux在HTTP+PHP服务中的安全权限修改
    基本运用(一个一个字读)
    C语言基础四(敲打键盘、寻找资料,循环语句)请一个个字读,助于您的学会机率
    C语言基础三(敲打键盘、寻找资料,循环语句)
    C语言基础二(敲打键盘、寻找资料)
  • 原文地址:https://www.cnblogs.com/xcsn/p/9340683.html
Copyright © 2020-2023  润新知