Routes .net core 2.0 API忽略无效路由的中间件
我有一些验证中间件,如果某些数据丢失,它会返回特定的响应 当我使用无效的URL进行调用时,中间件正在执行,错误会在响应中发送,但错误说明了验证问题,而不是URL无效 所以我的问题是,如何设置configure方法,使任何无效的URL都不执行中间件,并简单地返回404错误 以下是我的配置方法:Routes .net core 2.0 API忽略无效路由的中间件,routes,.net-core,middleware,Routes,.net Core,Middleware,我有一些验证中间件,如果某些数据丢失,它会返回特定的响应 当我使用无效的URL进行调用时,中间件正在执行,错误会在响应中发送,但错误说明了验证问题,而不是URL无效 所以我的问题是,如何设置configure方法,使任何无效的URL都不执行中间件,并简单地返回404错误 以下是我的配置方法: public void Configure(IApplicationBuilder app, IHostingEnvironment env)//, ITimingLogger timingLogg
public void Configure(IApplicationBuilder app, IHostingEnvironment env)//, ITimingLogger timingLogger
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
//app.Use(async (HttpContext context, Func<Task> next) =>
//{
// var timer = new Stopwatch();
// timer.Start();
// await next();
// timer.Stop();
// timingLogger.LogElapsedTime(context.Request.Method, context.Request.Path, timer.ElapsedMilliseconds);
//});
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMiddleware<RequestResponseLoggingMiddleware>();
app.UseMiddleware<UnhandledExceptionHandlerMiddleware>();
app.UseMiddleware<SubscriptionIdValidatorMiddleware>();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
public void配置(IApplicationBuilder应用程序,IHostingEnvironment环境)/,ITimingLogger计时记录器
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
//app.Use(异步(HttpContext上下文,Func-next)=>
//{
//var timer=新秒表();
//timer.Start();
//等待下一个();
//timer.Stop();
//TimeingLogger.LogElapsedTime(context.Request.Method、context.Request.Path、timer.elapsedMills);
//});
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMiddleware();
app.UseMiddleware();
app.UseMiddleware();
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller=Home}/{action=Index}/{id?}”);
});
}
感谢简单请求管道记录器
试试这个
app.Use(async (HttpContext context, Func<Task> next) =>
{
if (context== null) throw new ArgumentNullException(nameof(context));
var sw = Stopwatch.StartNew();
try
{
await next(context);
sw.Stop();
// do your logging work here
// for example
if (httpContext.Response?.StatusCode > 499)
{
// 500+ error code is a server error
// log the error
}
}
catch (Exception ex) when (LogException(context, sw, ex))
{
// never caught, because `LogException()` returns false.
}
}
请求管道 更直接地回答你的问题,而不仅仅是给你一段代码。我相信您误解了请求管道的行为。我会试着用你的代码来解释
app.Use(async (HttpContext context, Func<Task> next) =>
{
// every single request is currently hitting this middleware
// even regardless if the URI "exists" or not.
// to solve this you need to define what is a valid URI
// for example, validate the path
if (context.Request.Path != "foo")
{
return next(); // break out
}
// if we got this far, that means it was a valid URI
var timer = new Stopwatch();
timer.Start();
await next();
timer.Stop();
// do work
});
app.Use(异步(HttpContext-context,Func-next)=>
{
//当前,每个请求都会命中此中间件
//即使URI是否“存在”。
//要解决这个问题,您需要定义什么是有效的URI
//例如,验证路径
if(context.Request.Path!=“foo”)
{
return next();//中断
}
//如果我们走到这一步,这意味着它是一个有效的URI
var timer=新秒表();
timer.Start();
等待下一个();
timer.Stop();
//工作
});
我希望这能说明管道是如何工作的
请求管道按照您列出的顺序执行。目前,您当前的代码中没有定义什么是“有效”URI
谢谢。这就是我的想法,但我希望,既然.net知道路径无效,(我看到了404错误),也许有一个属性我可以检查,而不是在添加新端点时更新if语句。
app.Use(async (HttpContext context, Func<Task> next) =>
{
// every single request is currently hitting this middleware
// even regardless if the URI "exists" or not.
// to solve this you need to define what is a valid URI
// for example, validate the path
if (context.Request.Path != "foo")
{
return next(); // break out
}
// if we got this far, that means it was a valid URI
var timer = new Stopwatch();
timer.Start();
await next();
timer.Stop();
// do work
});