有意思的OWIN,附脱离iis的webapi

程序引用

using Owin;

using Microsoft.Owin.Hosting;

新建一个控制台程序

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

新建一个路由配置类 

RESTFULTStartup
public class RESTFULTStartup
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            // 创建 Web API 的配置
            var config = new HttpConfiguration();

            // Web API 路由
            config.MapHttpAttributeRoutes();

            //移除XML输出
            config.Formatters.Remove(config.Formatters.XmlFormatter);

            // 默认的 Web API 路由
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            // 将路由配置附加到 appBuilder
            appBuilder.UseWebApi(config);
        }
    }

新建一个自定义控制器类

RESController

特别说明:在控制器上标记[RoutePrefix("api/RES")]    在action方法上标记[Route("Test01")]

[RoutePrefix("api/RES")]
    public class RESController : ApiController
    {
        //[Route("Test01/{obj:int}")]
        [Route("Test01")]
        [HttpPost]
        public string Test01(dynamic obj)
        {
            StringWriter sw = new StringWriter();
            try
            {
                sw.WriteLine("Test01");
                sw.WriteLine("开始");
                sw.WriteLine(DateTime.Now.ToString());
                string requJSON = JsonConvert.SerializeObject(obj);
                sw.WriteLine(requJSON);
                sw.WriteLine("结束");
                var x = new
                {
                    code = "0",
                    desc = "success"
                };
                string jsonX = JsonConvert.SerializeObject(x);
                sw.WriteLine("返回值:"+ jsonX);
                return jsonX;
            }
            catch (Exception ex)
            {
                sw.WriteLine(ex.Message);
                sw.WriteLine(ex.StackTrace);
                throw;
            }
            finally
            {
                System.IO.File.WriteAllText("d:\\1fapi\\" + Guid.NewGuid() + ".txt", sw.ToString());
            }

        }

        //[Route("Test01/{obj:int}")]
        [Route("Test02")]
        [HttpPost]
        public string Test02(dynamic obj)
        {
            StringWriter sw = new StringWriter();
            try
            {
                sw.WriteLine("Test02");
                sw.WriteLine("开始");
                sw.WriteLine(DateTime.Now.ToString());
                string requJSON = JsonConvert.SerializeObject(obj);
                sw.WriteLine(requJSON);
                sw.WriteLine("结束");
                var x = new
                {
                    code = "0",
                    desc = "success"
                };
                string jsonX = JsonConvert.SerializeObject(x);
                sw.WriteLine("返回值:" + jsonX);
                return jsonX;
            }
            catch (Exception ex)
            {
                sw.WriteLine(ex.Message);
                sw.WriteLine(ex.StackTrace);
                throw;
            }
            finally
            {
                System.IO.File.WriteAllText("d:\\2fapi\\" + Guid.NewGuid() + ".txt", sw.ToString());
            }
        }
    }

最后运行,就可以实现webapi类似的需求

相关推荐