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