9
Answers

Unsupported Media Type Error [FormBody] ASP.NET Core 3.1 JSON

Photo of Muhammad Shaikh

Muhammad Shaikh

4y
981
1
I am using ASP.NET Core 3.1 MVC, When ever I am sending data to my HTTP Post method api, I am facing Unsupported media type.
 
I have used [FromBody] in my api.
 
Here is my api:
  1. [HttpPost]  
  2. [Route("addCourseWeek")]  
  3. public String AddCourseWeek([FromBody]CourseWeek data)  
  4. {  
  5. data.WeekName = String.Concat(data.WeekName.Where(c => !Char.IsWhiteSpace(c)));  
  6. if (data != null && !string.IsNullOrWhiteSpace(data.WeekName) && data.WeekName.Length > 0)  
  7. {  
  8. data.CreatedAt = DateTime.Now;  
  9. db.CourseWeeks.Add(data);  
  10. }  
  11. else  
  12. {  
  13. return Convert.ToString(ValidationProblem());  
  14. }  
  15. // db.CourseWeeks.Add(data);  
  16. try  
  17. {  
  18. db.SaveChanges();  
  19. }  
  20. catch  
  21. {  
  22. return Convert.ToString(NotFound());  
  23. }  
  24. return JsonConvert.SerializeObject(db.CourseWeeks,Formatting.Indented);  
  25. }  
Here is my html form:
  1. @model CourseGamePlay.Models.CourseWeek  
  2. @{  
  3. ViewBag.Title = "Admin Panel - Weeks";  
  4. Layout = "~/Views/Shared/_adminLayout.cshtml";  
  5. }  
  6. <h2>Add Weeks</h2>  
  7. <body>  
  8. <form method="post" action="/api/addCourseWeek">  
  9. <div class="form-horizontal">  
  10. <div class="form-group">  
  11. @Html.LabelFor(cw => cw.CourseId, htmlAttributes: new { @class = "control-label col-md-2" })  
  12. <div class="col-md-4">  
  13. @Html.EditorFor(cw => cw.CourseId, new { htmlAttributes = new { @class = "form-control" } })  
  14. @Html.ValidationMessageFor(cw => cw.CourseId, ""new { @class = "text-danger" })  
  15. </div>  
  16. </div>  
  17. <div class="form-group">  
  18. @Html.LabelFor(cw => cw.WeekName, htmlAttributes: new { @class = "control-label col-md-2" })  
  19. <div class="col-md-4">  
  20. @Html.EditorFor(cw => cw.WeekName, new { htmlAttributes = new { @class = "form-control" } })  
  21. @Html.ValidationMessageFor(cw => cw.WeekName, ""new { @class = "text-danger" })  
  22. </div>  
  23. </div>  
  24. <div class="form-group">  
  25. @Html.LabelFor(cw => cw.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })  
  26. <div class="col-md-4">  
  27. @Html.EditorFor(cw => cw.StartDate, new { htmlAttributes = new { @class = "form-control" } })  
  28. @Html.ValidationMessageFor(cw => cw.StartDate, ""new { @class = "text-danger" })  
  29. </div>  
  30. </div>  
  31. <div class="form-group">  
  32. @Html.LabelFor(cw => cw.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })  
  33. <div class="col-md-4">  
  34. @Html.EditorFor(cw => cw.EndDate, new { htmlAttributes = new { @class = "form-control" } })  
  35. @Html.ValidationMessageFor(cw => cw.EndDate, ""new { @class = "text-danger" })  
  36. </div>  
  37. </div>  
  38. <div class="form-group">  
  39. @Html.LabelFor(cw => cw.UpdatedAt, htmlAttributes: new { @class = "control-label col-md-2" })  
  40. <div class="col-md-4">  
  41. @Html.EditorFor(cw => cw.UpdatedAt, new { htmlAttributes = new { @class = "form-control" } })  
  42. @Html.ValidationMessageFor(cw => cw.UpdatedAt, ""new { @class = "text-danger" })  
  43. </div>  
  44. </div>  
  45. <div class="form-group">  
  46. <div class="col-md-offset-2 col-md-15">  
  47. <input type="submit" value="Submit" class="btn btn-primary" />  
  48. </div>  
  49. </div>  
  50. </div>  
  51. </form>  
  52. </body>  
Please help me out in it, if anybody knows it's solution. Thanks in advance.

Attachment: error.rar

Answers (9)