如何编写在Nancy中接受JSON的post方法,以及如何从C#client中调用它?
我用Nancy写了以下模块如何编写在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
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; }
}