-1
我正在嘗試創建ASP.NET Core Web API。我有郵政/獲取方法,按照以下:Post Method在ASP.NET Core中不工作
[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());
}
}
我已經試過[FromBody]爲好,但它也沒有工作
型號:
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"));
}
您的PersonModel的外觀如何? – jhen
如果我在Post方法中使用FromBody,我得到了同樣的錯誤,我在截圖 –
中顯示的是'Controllers'是否正確?或者它應該是你的路由配置中的'controller' – JamieD77