c#:使用Owin創(chuàng)建WebAPI

Owin簡介

open web Interface for doNet,在doNet Web服務器和doNet Web應用之間定義了一套標準的接口,其目的是為了實現(xiàn)服務器和應用之間的解耦,不再依賴于Windows和IIS

一、Owin使用

1.創(chuàng)建控制臺項目
2.NuGet添加Microsoft.AspNet.WebApi.Owin和Microsoft.Aspnet.WebApi.OwinSelfHost
3.添加Startup類

1.1、Startup

Startup是OWIN約定的,用于對OWIN做相關配置,代碼如下:

using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace OwinTest{
    public class Startup{
        public void Configuration(IAppBuilder appBuilder){
            //創(chuàng)建Web API 的配置
            var config = new HttpConfiguration();
            //啟動標記路由
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                name:"DefaultApi",
                routeTemplate:"api/{controller}/{id}"
            );
            //將路有配置附加到appBuilder
            appBuilder.UseWebApi(config);
        }
    }
}

1.2、Controllers類

新建一個queryController類,代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace OwinTest{
    public class queryController:ApiController{
        //get api
        public string Get(string id)
        {
            return id;
        }
        // POST api
         public string Post([FromBody] string value)
        {
            return value;
        }
        // PUT api
        public void Put(int id, string value)
        {
        }
        // DELETE api
        public void Delete(int id)
        {
        }
    }
}

自建一個PersonController類,代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Http;

namespance OwinTest{
    public class PersonController:ApiController{
        Person[] personList = new Person[] {
            new Person { Id= 1,Age = 2,Name="DANNY"},
            new Person { Id = 2,Age = 3,Name = "Danny123"},
            new Person { Id =3,Age = 4,Name = "dANNY456"}
        };
        
        [HttpGet]
        [Route("api/person/getAll")]
        public List<Person> GetListAll(){
            return personList.toList();
        }
        
        public List<Person> Get(string id){
            return personList.toList();
        }
    }
}

1.3、啟動WebAPI服務

Program代碼如下,啟動項目:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;

class Program
    {
        static void Main(string[] args)
        {
            string baseAddress = "http://localhost:9000/";
            using (Microsoft.Owin.Hosting.WebApp.Start<Startup>(url: baseAddress))
            {
                HttpClient client = new HttpClient();
                Console.WriteLine(baseAddress);
                Console.ReadLine();
            }
        }
    }

二、PostMan接口測試

2.1、自定義Route

調(diào)用PersonController的GetListAll方法

Get http://localhost:9000/api/person/getAll

調(diào)用PersonController的Get方法(其中ok只是參數(shù))

Get http://localhost:9000/api/Person/ok

調(diào)用queryController的Get方法

Get http://localhost:9000/api/query/abc

調(diào)用queryController的Post方法

Post http://localhost:9000/api/query
以下分別是string和Pserson的參數(shù)傳入:

string
stringdemo
Person
{
    "Id":1,
    "Name":"Danny",
    "Age":18
}

public class Person{
    public int Id{set;get;}
    public string Name{get;set;}
    public int Aget{set;get;}
}

三、參考

https://www.cnblogs.com/xishuai/p/aspnet_mvc_web_api_httpclient_json_frombody_post.html
http://www.cnblogs.com/dudu/archive/2012/05/11/asp_net_webapi_httpclient.html
https://www.cnblogs.com/webenh/p/8979510.html

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容