I trust you doing good, I am new to .net core API
, I am doing a model validation, seems like it does not work,
Controller Action :
[HttpPost]
[ValidateModel]
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(ActionLog))]
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ActionLog))]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> CreateAsync([FromBody] ActionLog document)
{
try
{
if (ModelState.IsValid)
{
var result = await this.documentRepository.CreateAsync(document);
return this.StatusCode(StatusCodes.Status201Created,document.Id);
}
else
{
return this.StatusCode(StatusCodes.Status400BadRequest);
}
}
catch (Exception)
{
return this.StatusCode(StatusCodes.Status500InternalServerError, "Internal Server Error");
}
}
Model Class
public class ActionLog : ILogDocument
{
/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>
/// The identifier.
/// </value>
[BsonElement("id")]
[BsonId(IdGenerator = typeof(ObjectIdGenerator))]
public ObjectId Id { get; set; }
/// <summary>
/// Gets or sets the source identifier.
/// </summary>
/// <value>
/// The source identifier. Used to identify which source the log came from.
/// </value>
[BsonRequired]
[BsonElement("sourceId")]
public string SourceId { get; set; }
/// <summary>
/// Gets or sets the keywords.
/// </summary>
/// <value>
/// The keywords associated to the log.
/// Those keywords allow for faster searches compared to a full-text search on the payload.
/// This should be used as much as possible for common actions like: subject, type of action or target resource.
/// </value>
[BsonElement("keywords")]
public string[] Keywords { get; set; } = Array.Empty<string>();
/// <summary>
/// Gets or sets the payload of the action log (usually more detailed data).
/// </summary>
/// <value>
/// The payload.
/// </value>
[BsonElement("payload")]
public string Payload { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the date of event occurrence.
/// </summary>
/// <value>
/// The date.
/// </value>
[BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
[BsonElement("date")]
public DateTime Date { get; set; } = DateTime.UtcNow;
}
Apart from that I have not coded any configurations in other classes or configuration, It returns 201 for every request.