Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写在Nancy中接受JSON的post方法,以及如何从C#client中调用它?_C#_Rest_Mono_Nancy - Fatal编程技术网

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

如何编写在Nancy中接受JSON的post方法,以及如何从C#client中调用它?,c#,rest,mono,nancy,C#,Rest,Mono,Nancy,我用Nancy写了以下模块 public class CategoryModule : NancyModule { public CategoryModule() { //At this moment just Show Hello world Get["/"] = _ => { return "Nancy says hello!"; }; //Get["/"] = parameters => "Hello Wo

我用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;
         };
     }
}
下面是类别模型

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

}

如果您想用POSTMAN发布JSON,您应该添加JSON内容类型的标题,比如(第三段)。设置邮递员:

  • 将主机设置为
    http://192.168.1.4:8888/Categories
    并选择
    POST
  • 添加带有
    Content-Type
    application/json
    作为标题和值的标题 分别
  • raw
    JSON
    设置为类型
  • 将此
    {“CategoryName”:“something”}
    放在下面的文本字段中
  • 点击“发送”

  • 请添加您的
    类别
    类别代码。@RomanDibikhin谢谢。我也更新了类别代码!好的,明天我将尝试重现您的问题。@RomanDibikhin谢谢@Thabo感谢您的反馈;)互相帮助很酷。
        public class Category
    {
        public Guid PK_CategoryId { get; set; }
        public string CategoryName { get; set; }
    
    }