如何在 Nancy 中编写接受 JSON 的 post 方法以及如何从 C# 客户端调用它?

How to write a post method which accepts JSON in Nancy and how to call it form a C# client?

我在 Nancy 中编写了以下模块

    public class CategoryModule : NancyModule
{
    public CategoryModule()
    {
        //At this moment just Show Hello world
        Get["/"] = _ => { return "Nancy says hello!"; };
        //Get["/"] = parameters => "Hello World!";
        GetCategories();
        SetCategory();
    }


     void GetCategories()
    {
        Get["/Catergories"] = _ =>
        {

            var catergoryRepository = new CategoryRepository();
            var categorycollection = catergoryRepository.GetCategoryInfo();
            return Negotiate.WithStatusCode(HttpStatusCode.OK).WithModel(categorycollection.ToArray());
        };
    }

     void SetCategory()
     {
         Post["/Catergories/{categryName:string}"] = _ =>
         {
             var catergoryModel = this.Bind<Category>();
             catergoryModel.PK_CategoryId = Guid.NewGuid();
             catergoryModel.CategoryName = _;
             return HttpStatusCode.OK;
         };
     }
}

我正在使用 chrome POSTMAN 来测试 Module.I 如果我调用 可以在 "GetCategories()" 中获取断点]http://192.168.1.4:8888/Categories 在邮递员中。 但是如果我调用 http://192.168.1.4:8888/Catergories/categryName=test,我无法在 SetCategory() 中获得断点。我是 Nancy 的新手,不确定我的 post 方法是否正确。

谁能提供

  1. 接受 Jason 作为参数的 Post 方法示例
  2. 从客户端调用它的示例

我在他们的文档中找不到上面的简单示例。

我正在使用自托管环境,我使用以下代码托管 nancy

var server = new Nancy.Hosting.Self.NancyHost(new Uri("http://192.168.1.4:8888"));

下面是品类模型

    public class Category
{
    public Guid PK_CategoryId { get; set; }
    public string CategoryName { get; set; }

}

如果你想 post JSON 与 POSTMAN 你应该添加 headers 与 JSON 内容类型,如 docs 所说(第三段) .设置邮递员:

  1. 将您的主机设置为 http://192.168.1.4:8888/Categories 和 select POST
  2. 将 headers 添加为 Content-Typeapplication/json 作为 header 和值 分别。
  3. 设置 raw 并设置 JSON 作为类型。
  4. { "CategoryName": "something" } 按原样放在下面的文本字段中。
  5. 命中"Send"。