I am using ASP.NET Core 3.1 MVC, Whenever I am sending data to my HTTP Post method api, I am facing Unsupported media type.
Swagger UI, it is not showing data in JSON format.
I want to show data in JSON format, I am stuck on it, anybody have solution for it.
Thanks in advance.
Here is my api:
[HttpPost]
[Route("addCourseWeek")]
public String AddCourseWeek([FromBody] CourseWeek data)
{
data.WeekName = String.Concat(data.WeekName.Where(c => !Char.IsWhiteSpace(c)));
if (data != null && !string.IsNullOrWhiteSpace(data.WeekName) && data.WeekName.Length > 0)
{
data.CreatedAt = DateTime.Now;
db.CourseWeeks.Add(data);
}
else
{
return Convert.ToString(ValidationProblem());
}
// db.CourseWeeks.Add(data);
try
{
db.SaveChanges();
}
catch
{
return Convert.ToString(NotFound());
}
return JsonConvert.SerializeObject(db.CourseWeeks, Formatting.Indented);
}
Here is my .cshtml :
@model CourseGamePlay.Models.CourseWeek
@{ ViewBag.Title = "Admin Panel - Weeks";
Layout = "~/Views/Shared/_adminLayout.cshtml"; }
<h2>Add Weeks</h2>
<body>
<form method="post" action="/api/addCourseWeek">
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(cw => cw.CourseId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(cw => cw.CourseId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(cw => cw.CourseId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(cw => cw.WeekName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(cw => cw.WeekName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(cw => cw.WeekName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(cw => cw.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(cw => cw.StartDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(cw => cw.StartDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(cw => cw.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(cw => cw.EndDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(cw => cw.EndDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(cw => cw.UpdatedAt, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(cw => cw.UpdatedAt, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(cw => cw.UpdatedAt, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-15">
<input type="submit" value="Submit" class="btn btn-primary" />
</div>
</div>
</div>
</form>
</body>