我真的很难在 url 中发出基本的 post 请求来支持 web api 教程。
我想在浏览器中做这样的事情:http://localhost:59445/api/group/post/?newvalue=test并获取帖子进行注册。但是我似乎无法正确形成请求。执行此操作的正确方法是什么?
我收到的错误是:
{"Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Get(Int32)' in 'twin_groupapi.Controllers.GroupController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."}
我的模型:
public class Group
{
public Int32 GroupID { get; set; }
public Int32 SchoolID { get; set; }
public string GroupName { get; set; }
}
路由:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Controller :
//[Route("api/Group/Post")]
[HttpPost]
public void Post([FromUri] string NewValue)
{
string newstring = NewValue;
}
最佳答案
在浏览器中点击 URL 只会发出 GET 请求。
您可以:
- 创建一个简单的
<form>
其方法设置为POST
并形成输入以输入您要发送的值(如NewValue
),或者 - 使用您喜欢的框架编写一些 JavaScript 来创建 AJAX POST 请求,或者
- 使用类似 Postman 的工具设置 POST 请求、调用它并检查结果。
关于c# - 制作一个简单的 Web API 发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41153986/