Asp.net core 将ASP.NET核心OData与FluentValidation集成
我试图让OData使用FluentValidation验证我的模型。但我似乎找不到一个方法告诉OData使用它,因为它会给我这个回答Asp.net core 将ASP.NET核心OData与FluentValidation集成,asp.net-core,odata,fluentvalidation,Asp.net Core,Odata,Fluentvalidation,我试图让OData使用FluentValidation验证我的模型。但我似乎找不到一个方法告诉OData使用它,因为它会给我这个回答 { "error": { "code": "", "message": "The input was not valid.", "details": [ { "code": "", "message": "The inpu
{
"error": {
"code": "",
"message": "The input was not valid.",
"details": [
{
"code": "",
"message": "The input was not valid."
}
]
}
}
发送此有效负载时
{
"id": "",
"name": "",
"author": ""
}
控制器post操作中的Blog
参数也是null
我正在使用
- netcoreapp3.1
- Microsoft.AspNetCore.OData Version=“7.4.0-beta”
- FluentValidation.AspNetCore Version=“9.0.0-preview3”
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public string Author { get; set; }
}
验证程序
public class BlogValidator : AbstractValidator<Blog> {
public BlogValidator() {
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Name).NotEmpty();
RuleFor(x => x.Author).NotEmpty();
}
}
[Route("odata/[controller]s")]
public class BlogController : ODataController
{
private readonly MyContext _context;
public BlogController(MyContext context)
{
_context = context;
}
[HttpGet]
[EnableQuery]
public IActionResult Get() {...}
[HttpPost]
public async Task<IActionResult> Post([FromBody] Blog blog) {
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// omitted...
}
}
公共类BlogValidator:AbstractValidator{
公共博客验证程序(){
RuleFor(x=>x.Id).NotEmpty();
RuleFor(x=>x.Name).NotEmpty();
RuleFor(x=>x.Author).NotEmpty();
}
}
控制器
public class BlogValidator : AbstractValidator<Blog> {
public BlogValidator() {
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Name).NotEmpty();
RuleFor(x => x.Author).NotEmpty();
}
}
[Route("odata/[controller]s")]
public class BlogController : ODataController
{
private readonly MyContext _context;
public BlogController(MyContext context)
{
_context = context;
}
[HttpGet]
[EnableQuery]
public IActionResult Get() {...}
[HttpPost]
public async Task<IActionResult> Post([FromBody] Blog blog) {
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// omitted...
}
}
[路由(“odata/[controller]s”)]
公共类BlogController:ODataController
{
私有只读MyContext\u上下文;
公共博客控制器(MyContext上下文)
{
_上下文=上下文;
}
[HttpGet]
[启用查询]
public IActionResult Get(){…}
[HttpPost]
公共异步任务帖子([FromBody]博客){
如果(!ModelState.IsValid)
{
返回请求(ModelState);
}
//省略。。。
}
}
启动
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MMContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyDatabase")));
services.AddControllersWithViews().AddFluentValidation(options => options.RegisterValidatorsFromAssembly(Assembly.GetExecutingAssembly()));
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "../frontend/dist";
});
services.AddOData();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}
app.UseRouting();
var model = EdmModelBuilder.GetEdmModel();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.Select().Filter().OrderBy().Count().MaxTop(50);
endpoints.MapODataRoute("odata", "odata", model);
});
app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath = "..\\frontend";
if (env.IsDevelopment())
{
spa.UseProxyToSpaDevelopmentServer("http://localhost:4200");
}
});
}
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(options=>options.UseSqlServer(Configuration.GetConnectionString(“MyDatabase”));
services.AddControllersWithViews().AddFluentValidation(options=>options.RegisterValidatorsFromAssembly(Assembly.getExecutionGassembly());
//在生产中,角度文件将从此目录中提供
services.AddSpaStaticFiles(配置=>
{
configuration.RootPath=“../frontend/dist”;
});
services.AddOData();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
如果(!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}
app.UseRouting();
var model=EdmModelBuilder.GetEdmModel();
app.UseEndpoints(端点=>
{
endpoints.MapControllers();
endpoints.Select().Filter().OrderBy().Count().MaxTop(50);
MapODataRoute(“odata”,“odata”,模型);
});
app.UseSpa(spa=>
{
//要从ASP.NET Core了解更多有关提供角度SPA的选项,
//看https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath=“..\\frontend”;
if(env.IsDevelopment())
{
spa.UseProxyToSpadDevelopmentServer(“http://localhost:4200");
}
});
}
}