0

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.

reference : https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api

ArunPratap
  • 4,816
  • 7
  • 25
  • 43
UtpMahesh
  • 410
  • 10
  • 25
  • It has been replaced by [ApiController] – Edunikki Dec 05 '19 at 11:43
  • @Edunikki Thanks you, it does not work, further, In debugging it always ModelState.IsValid == true – UtpMahesh Dec 05 '19 at 13:50
  • @Edunikki I guess I found the issue for this issue, Validation pass for the NULL values, Is there any way that we can define the NOT NULL with [BsonRequired] data annotation ? – UtpMahesh Dec 08 '19 at 05:41

0 Answers0