1 Star 2 Fork 0

花花世界迷人眼 / ConsoleWebApI

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
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();
    }

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

空文件

简介

用Console 或 Winform 实现webapi服务 展开 收起
C#
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/vblegend/ConsoleWebApI.git
git@gitee.com:vblegend/ConsoleWebApI.git
vblegend
ConsoleWebApI
ConsoleWebApI
master

搜索帮助