如何通过 axios get 方法将对象从 React 发送到 .NET API?
How to send object to through axios get method from react to .NET API?
我有 .NET API
示例:
[Route("[controller]")]
[ApiController]
public class UserController : ControllerBase
{
[HttpGet("GetUser")]
public ActionResult GetUserDetails(User userReq)
{
var response = service.GetUser(userReq);
return Ok(response);
}
}
和用户 class 示例:
public class User
{
public int? ID { get; set; }
public string Name { get; set; }
public int? Age { get; set; }
public char? Gender { get; set; }
public string Email { get; set; }
public int? Phone { get; set; }
}
我在 React 中通过 axios 以下面的方式调用 API,示例:
axios.get(String(APIUrls.GetUser),
{
params: {
ID: null,
Name: null,
Age: null,
Gender: null,
Email: null,
Phone: null
},
}).then(response => {
console.log(response.data);
})
.catch((err) => {
console.log(err);
});
我可以通过邮递员获得回复,但是当我从我的应用程序尝试时它不起作用。
错误:
Status Code: 415
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"xxxxxxxxxxxx:00000005"}
添加[FromQuery]
属性指定来源:
[HttpGet("GetUser")]
public ActionResult GetUserDetails([FromQuery] User userReq)
{
//...
}
我有 .NET API 示例:
[Route("[controller]")]
[ApiController]
public class UserController : ControllerBase
{
[HttpGet("GetUser")]
public ActionResult GetUserDetails(User userReq)
{
var response = service.GetUser(userReq);
return Ok(response);
}
}
和用户 class 示例:
public class User
{
public int? ID { get; set; }
public string Name { get; set; }
public int? Age { get; set; }
public char? Gender { get; set; }
public string Email { get; set; }
public int? Phone { get; set; }
}
我在 React 中通过 axios 以下面的方式调用 API,示例:
axios.get(String(APIUrls.GetUser),
{
params: {
ID: null,
Name: null,
Age: null,
Gender: null,
Email: null,
Phone: null
},
}).then(response => {
console.log(response.data);
})
.catch((err) => {
console.log(err);
});
我可以通过邮递员获得回复,但是当我从我的应用程序尝试时它不起作用。
错误:
Status Code: 415
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"xxxxxxxxxxxx:00000005"}
添加[FromQuery]
属性指定来源:
[HttpGet("GetUser")]
public ActionResult GetUserDetails([FromQuery] User userReq)
{
//...
}