netframework 控制台 owin添加WebApi 和Swagger

1、安装依赖

Microsoft.AspNet.WebApi

Microsoft.AspNet.WebApi.Owin

Microsoft.Owin.Hosting

Microsoft.Owin.Host.HttpListener

Swashbuckle

2、Startup.cs

using Owin;
using Swashbuckle.Application;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
 
namespace ConsoleAPI
{
    public class Startup
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            HttpConfiguration config = new HttpConfiguration();
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}",
                defaults: new { id = RouteParameter.Optional }
            );
            config
                .EnableSwagger(c => c.SingleApiVersion("v1", "A title for your API"))
                .EnableSwaggerUi();
 
            //清除xml格式,使用json格式
            config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            config.Formatters.Add(new JsonMediaTypeFormatter());
 
            appBuilder.UseWebApi(config);
        }
    }
}

3、program.cs

using Microsoft.Owin.Hosting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAPI
{
    class Program
    {
        static void Main(string[] args)
        {

            StartOptions options = new StartOptions();
            options.Urls.Add("http://localhost:10010");
            options.Urls.Add("http://127.0.0.1:10010");
            options.Urls.Add(string.Format("http://{0}:10010", Environment.MachineName));
            List<string> ips = new List<string>();
            IPAddress[] ipadrlist = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ipa in ipadrlist)
            {
                if (ipa.AddressFamily == AddressFamily.InterNetwork)
                    options.Urls.Add(string.Format("http://{0}:10010", ipa.ToString()));
            }
            using (WebApp.Start<Startup>(options))
            {
                Console.WriteLine("server started...");

                Console.ReadLine();
            }
        }

    }
}

4、控制器

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

namespace ConsoleAPI
{

    public class Product
    {
        public string Name { get; set; }
        public string Price { get; set; }
    }
    [RoutePrefix("api/products")]
    public class ProductsController : ApiController
    {
        static List<Product> products = new List<Product>() {
        new Product(){Name="product1",Price="6.66"},
        new Product(){Name="product2",Price="8.88"}
        };

        [HttpGet]
        [Route("get")]
        public IEnumerable<Product> Get()
        {
            return products;
        }

        [HttpGet]
        [Route("get2")]
        public string Get(int id)
        {
            return JsonConvert.SerializeObject(products) + "|||||||||||||||||" + id;
        }
    }

}

5、启动控制台,浏览器访问http://localhost:10010/swagger

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值