C# ValidateModel无法使用asp.net core 3.0
我相信你做得很好,我是新来的C# ValidateModel无法使用asp.net core 3.0,c#,asp.net-core,.net-core,bson,model-validation,C#,Asp.net Core,.net Core,Bson,Model Validation,我相信你做得很好,我是新来的.net core API,我正在做一个模型验证,似乎它不起作用 控制器操作: [HttpPost] [ValidateModel] [ProducesResponseType(StatusCodes.Status201Created, Type = typeof(ActionLog))] [ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ActionLog
.net core API
,我正在做一个模型验证,似乎它不起作用
控制器操作:
[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");
}
}
[HttpPost]
[验证模型]
[产品响应类型(StatusCodes.Status201Created,Type=typeof(ActionLog))]
[产品响应类型(StatusCodes.Status400BadRequest,Type=typeof(ActionLog))]
[产品响应类型(StatusCodes.Status401Unauthorized)]
公共异步任务CreateAsync([FromBody]操作日志文档)
{
尝试
{
if(ModelState.IsValid)
{
var result=wait this.documentRepository.CreateAsync(文档);
返回此.StatusCode(StatusCodes.Status201Created,document.Id);
}
其他的
{
返回此.StatusCode(StatusCodes.Status400BadRequest);
}
}
捕获(例外)
{
返回此.StatusCode(StatusCodes.Status500InternalServerError,“内部服务器错误”);
}
}
模范班
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;
}
public类操作日志:ILogDocument
{
///
///获取或设置标识符。
///
///
///标识符。
///
[b单一元素(“id”)]
[BsonId(IdGenerator=typeof(ObjectedGenerator))]
公共对象Id{get;set;}
///
///获取或设置源标识符。
///
///
///源标识符。用于标识日志来自哪个源。
///
[b必须填写]
[BsonElement(“源ID”)]
公共字符串SourceId{get;set;}
///
///获取或设置关键字。
///
///
///与日志关联的关键字。
///与有效负载上的全文搜索相比,这些关键字允许更快的搜索。
///这应该尽可能多地用于常见操作,如:主题、操作类型或目标资源。
///
[b单一元素(“关键字”)]
公共字符串[]关键字{get;set;}=Array.Empty();
///
///获取或设置操作日志的有效负载(通常是更详细的数据)。
///
///
///有效载荷。
///
[b单一元素(“有效载荷”)]
公共字符串有效负载{get;set;}=string.Empty;
///
///获取或设置事件发生的日期。
///
///
///日期。
///
[BsonDateTimeOptions(种类=日期时间种类.Utc)]
[b单一元素(“日期”)]
公共日期时间日期{get;set;}=DateTime.UtcNow;
}
除此之外,我没有在其他类或配置中编码任何配置,它为每个请求返回201
参考:它已被[ApiController]@Edunikki替换。谢谢你,而且,在调试它时,它总是ModelState.IsValid==true@Edunikki我想我找到了这个问题的症结所在,空值的验证通过,我们有没有办法用[BsonRequired]数据注释定义NOTNULL?