I am trying to create ASP.NET Core Web API. I have Post/Get method as per below:
[HttpGet]
[Route("api/getProffessionByID/{id}")]
public IHttpActionResult getProffessionByID(Int64 id)
{
AllProffession model = new AllProffession();
try
{
var result = model.GetAllProffession.Where(i => i.id == id).FirstOrDefault();
return Ok(result);
}
catch (Exception ex)
{
return InternalServerError("Something Went Wrong : " + ex.ToString());
}
}
[HttpPost]
[Route("api/savePerson")]
private IHttpActionResult savePerson(PersonModel model)
{
try
{
if (model.name != string.Empty || model.weight != 0 || model.height != 0 || model.proffession != 0)
{
Guid guid = Guid.NewGuid();
Random random = new Random();
int i = random.Next();
model.id = i;
return Ok(model);
}
else
{
return BadRequest();
}
}
catch (Exception ex)
{
return InternalServerError("Something Went Wrong : " + ex.ToString());
}
}
My get Method is working all fine from url as well as Postman.
I've tried [FromBody] As well, but it is also not working
Model:
public class PersonModel
{
public int id { get; set; }
public string name { get; set; }
public int weight { get; set; }
public int height { get; set; }
public int proffession { get; set; }
}
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { Controllers = "API", id = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
Maybe you should change your method to public instead of private .
[HttpPost]
[Route("api/savePerson")]
public IHttpActionResult savePerson(PersonModel model) {
//...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.