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

你可能感兴趣的:(代码,c#,json,microsoft)