搭建webapi2 Owin环境

搭建环境以及基本配置

  1. 首先创建一个空白Web App 项目.就是选Empty那个,记得不要勾host on cloud/云托管
  2. 然后通过nuget安装下面packages.config内容.
  <package id="EntityFramework" version="6.1.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.Cors" version="5.0.0" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.Owin" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Cors" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
  <package id="Owin" version="1.0" targetFramework="net452" />
  1. 接着配置Startup.cs,初始化WebApi
        var config = new HttpConfiguration();
        config.MapHttpAttributeRoutes();//开启标注路由
        config.Services.Replace(typeof(IContentNegotiator), new JsonFirstContentNegotiator());//内容协商为jsonFirst,这样,默认使用Json,但是又不影响其他格式.
        app.UseWebApi(config);//go
        //其他Owin中间件相关知识,参考MSDN
    public class JsonFirstContentNegotiator : DefaultContentNegotiator
    {
        private readonly JsonMediaTypeFormatter _jsonFormatter;

        public JsonFirstContentNegotiator(bool indented = true, bool camelcase = true)
        {
            _jsonFormatter = new JsonMediaTypeFormatter();
            var serializerSettings = _jsonFormatter.SerializerSettings;
            if (indented)
                serializerSettings.Formatting = Formatting.Indented;
            if (!camelcase)
                return;
            serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        }

        protected override MediaTypeFormatterMatch SelectResponseMediaTypeFormatter(ICollection<MediaTypeFormatterMatch> matches)
        {
            return matches.FirstOrDefault(m => m.Formatter is JsonMediaTypeFormatter) ?? base.SelectResponseMediaTypeFormatter(matches);
        }
    }
  1. 接着可以输出Hello World了,IndexController.cs
    public class IndexController : ApiController
    {
        [HttpGet]
        [Route()]
        public string HelloWorld()
        {
            return "HelloWorld";
        }

        [HttpGet]
        [Route("objectInfo")]
        public Dto GetObjInfo()
        {
            return new Dto{
                Name = "songtin.huang",
                Age = 24
            }//根据前面配置的JsonFirstContentNegotiator,WebApi会自动返回json对象
        }
    }
    public class Dto
    {
        public string Name{get;set;}
        public int Age{get;set;}
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,947评论 18 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,766评论 18 399
  • 前言 路由是指Web API如何匹配到具体的动作。Web API 2支持一个新的路由类型,它被称为属性路由。正如其...
    yaoshiyou阅读 1,538评论 0 2
  • 从es5开始,js中开始拥有了一种描述属性特征的特性(即属性描述符)。根据特性的不同,可以把属性分成两种类型:数据...
    hope7th阅读 738评论 0 0
  • 几天前,一件发生在长沙的惨案引发了各媒体的关注:一个5岁的男孩把一个2岁的女孩关进电梯,导致女孩坠楼身亡。 惨案发...
    浮生漫读阅读 2,168评论 1 4