I have controller which is
我有控制器
[HttpGet]
public IHttpActionResult GetUserRights(int id)
{
var rights = _rightsService.GetUserRights(id);
if (rights == null)
{
return new HttpActionResult(HttpStatusCode.BadRequest, "User not found");
}
return Ok(rights);
}
[HttpPost]
public IHttpActionResult SetUserRights(int id, IEnumerable<EditRightViewModel> rights)
{
Result result = _rightsService.SetUserRights(id, rights);
if (!result.IsSuccess)
{
return new HttpActionResult(HttpStatusCode.InternalServerError, result.Message);
}
return Ok(result.Data);
}
[HttpPost]
public IHttpActionResult SetUserRights(int id, int RightId, bool Enable)
{
Result result = _rightsService.SetUserRights(id, new List<EditRightViewModel>
{
new EditRightViewModel
{
RightId = RightId,
Enable = Enable
}
});
if (!result.IsSuccess)
{
return new HttpActionResult(HttpStatusCode.InternalServerError, result.Message);
}
return Ok(result.Data);
}
The get request works fine but the post returns 404. The request is
get请求正常,但帖子返回404.请求是
User-Agent: Fiddler
Host: localhost:577
Content-Length: 44
Content-Type: application/json; charset=utf-8
{
"id":1,
"RightId":1,
"Enable":"true"
}
Originally I want to send data to the post action that has a list as parameter. I thought that reason why it returns 404 is that json doesn't fits the function's arguments. So I simplified json and action parameters but it still won't work. Maybe someone can tell what's the reason of the problem and how to write a correct json to send list as a parameter?
最初我想将数据发送到具有列表作为参数的post动作。我认为它返回404的原因是json不适合函数的参数。所以我简化了json和动作参数,但它仍然不起作用。也许有人可以告诉问题的原因是什么以及如何编写一个正确的json来发送列表作为参数?
2 个解决方案
#1
0
404 Errors mostly occur when you Method signature does not match your POST
data or when your Routing is not configured correctly.
404错误主要发生在方法签名与POST数据不匹配或路由配置不正确时。
Have a look at my answer here - Webservice return 404
看看我的答案 - Webservice返回404
I would change the Method signature to accept a class instead.
我会改变Method签名来接受一个类。
public class UserRights
{
public int id {get;set;}
public int RightId {get;set;}
public bool Enable {get;set;}
}
#2
0
Post Actions can not have most one argument in body. Create a class with the parameters you need. ASP NET will do the binding.
Post Actions在body中不能包含大多数参数。使用您需要的参数创建一个类。 ASP NET会做绑定。
public IHttpActionResult SetUserRights(Right right){ /* ... */ }
public class Right {
public int id { get; set; }
public int RightId { get; set; }
public bool Enable { get; set; }
}
Try this.
#1
0
404 Errors mostly occur when you Method signature does not match your POST
data or when your Routing is not configured correctly.
404错误主要发生在方法签名与POST数据不匹配或路由配置不正确时。
Have a look at my answer here - Webservice return 404
看看我的答案 - Webservice返回404
I would change the Method signature to accept a class instead.
我会改变Method签名来接受一个类。
public class UserRights
{
public int id {get;set;}
public int RightId {get;set;}
public bool Enable {get;set;}
}
#2
0
Post Actions can not have most one argument in body. Create a class with the parameters you need. ASP NET will do the binding.
Post Actions在body中不能包含大多数参数。使用您需要的参数创建一个类。 ASP NET会做绑定。
public IHttpActionResult SetUserRights(Right right){ /* ... */ }
public class Right {
public int id { get; set; }
public int RightId { get; set; }
public bool Enable { get; set; }
}
Try this.