【问题标题】:Handle POST and GET in C#在 C# 中处理 POST 和 GET
【发布时间】:2015-11-12 00:45:43
【问题描述】:

我正在尝试开发一个处理 POST 和 GET 请求的程序。

我花了无数个小时在网上搜索不依赖于 ASP.NET 的教程我不想只使用标准 C# 的 ASP.NET

我怎样才能做到这一点?我得到的最远的是:

if (HttpMethod.ContentType == "POST") {
  // Code here
}

我在http://localhost:8080/ 上创建了一个函数 HttpListen 服务器,它发送响应。

我要找的是你做http://localhost:8080/?method=val1&results=val2

谢谢,布朗。

【问题讨论】:

  • 您正在寻找的是在 c# 中创建一个 http 库。你搜索过类似的东西吗?
  • 我已经搜索了所有与 Http 相关的东西。我似乎找不到任何东西。
  • 好吧,您必须启动一个侦听端口 80 的应用程序,然后研究http 协议,然后从那里开始。您放在那里的代码 sn-p 并没有真正告诉我们有关您当前状态的任何信息……它根本与处理请求无关,它是您获得后编写的代码的一部分服务器实现..
  • 我已经完成了。我已经制作了一个功能强大的 Http Listen 服务器。
  • 你检查过这个:stackoverflow.com/questions/11394229/… 吗?如果它与你的担忧有关?

标签: c#


【解决方案1】:

您正在寻找不是 ASP.NET 的 HTTP 服务器库?

尴尬地绕过“ASP.NET 出了什么问题?”这个问题......

Nancy 是一个很棒的轻量级开源库。你可以找到一些samples on github,尽管样本有点重。如果您正在寻找一个非常基本的设置,您可以使用几十行代码。一个很好的例子是基于控制台的self-hosted 示例。

// add such a module class to your assembly, Nancy should find it automagically
public class ResourceModule : NancyModule
{
    public ResourceModule() : base("/products")
    {
        // would capture routes to /products/list sent as a GET request
        Get["/list"] = parameters => {
            return "The list of products";
        };
    }
}

// then start the host
using (var host = new NancyHost(new Uri("http://localhost:1234")))
{
   host.Start();
   // do whatever other work needed here because once the host is disposed of the server is gone 
   Console.ReadLine();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多