webApi的控制台服务


 1.新建console项目,引用 下面包

 

2.新建Controller

    public class UserController : ApiController
    {
        public IEnumerable<string> Get()
        {
            return new string[] { "Aven", "Grace" };
        }
    }

 

3.在main里设置

 static void Main(string[] args)
        {
            //默认使用json返回数据而不论请求是不是application/xml
            //GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            var config=new HttpSelfHostConfiguration("http://localhost:8080");
            
            config.Routes.MapHttpRoute("ApiDefault", "api/{controller}/{id}", new {id = RouteParameter.Optional});
            using (var server = new HttpSelfHostServer(config))
            {
                server.OpenAsync().Wait();
                Console.WriteLine("Press Enter To Quit");
                Console.ReadLine();
            }
        }

 

4.然后启动后就可以直接用浏览器访问了

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM