# ConsoleWebApI
**Repository Path**: vblegend/ConsoleWebApI
## Basic Information
- **Project Name**: ConsoleWebApI
- **Description**: 用Console 或 Winform 实现webapi服务
- **Primary Language**: C#
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 2
- **Forks**: 0
- **Created**: 2018-01-02
- **Last Updated**: 2022-07-28
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# ConsoleWebApI
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();
}
///
/// 测试webapi
///
public class TestController : ApiController
{
// GET api/
public IEnumerable Get()
{
return new string[] { "value1", "value2" };
}
// GET api//5
public Demo Get(int id)
{
return new Demo() { url="www.baidu.com",appName = "baidu" };
}
// POST api/
public void Post([FromBody]Demo value)
{
Console.WriteLine(value.ToString());
}
// PUT api//5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api//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);
}
}