1 Star 1 Fork 0

说好的幸福呢? / ConsoleWebApI

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

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();
    }

/// <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);
    }
}

Comments ( 0 )

Sign in for post a comment

About

用Console 或 Winform 实现webapi服务 spread retract
C#
Cancel

Releases

No release

Contributors

All

Activities

load more
can not load any more
C#
1
https://gitee.com/vblegend/ConsoleWebApI.git
git@gitee.com:vblegend/ConsoleWebApI.git
vblegend
ConsoleWebApI
ConsoleWebApI
master

Search