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.然后启动后就可以直接用浏览器访问了