代码拉取完成,页面将自动刷新
public static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:3333");
config.Routes.MapHttpRoute("default", "api/{controller}/{id}", new { id = RouteParameter.Optional });
//清掉Xml选择器
config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
//默认返回 json
config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("datatype", "json", "application/json"));
//可以返回 xml
config.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("datatype", "xml", "application/xml"));
var server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
Console.WriteLine("Server is opened");
Console.ReadKey();
}
/// <summary>
/// 测试webapi
/// </summary>
public class TestController : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5
public Demo Get(int id)
{
return new Demo() { url="www.baidu.com",appName = "baidu" };
}
// POST api/<controller>
public void Post([FromBody]Demo value)
{
Console.WriteLine(value.ToString());
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
public class Demo
{
public string appName { get; set; }
public string url { get; set; }
public override string ToString()
{
return string.Format("appName:{0},url:{1},datetime:{2}", this.appName, this.url, DateTime.Now);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。