[英]How can I validate a URL before the request gets to the controller
Our service uses ASP.NET Core and in the application pipeline, we have several middlewares which are configured in StartUp.cs Configure(IApplicationBuilder app)
method. 我们的服务使用ASP.NET Core,在应用程序管道中,我们有几种中间件,这些中间件在StartUp.cs
Configure(IApplicationBuilder app)
方法中进行Configure(IApplicationBuilder app)
。
The middlewares are added by this method: app.UseMiddleware<Type>();
通过这种方法添加中间件:
app.UseMiddleware<Type>();
I would like to validate the HttpContext.Request.Path
and make sure it can hit one of the controllers. 我想验证
HttpContext.Request.Path
并确保它可以命中控制器之一。 How can I get the list of available routes (controller path) in the middleware code or is there even a simpler way to see if this certain request path will hit one of the registered controller? 如何在中间件代码中获取可用路由列表(控制器路径),或者还有一种更简单的方法来查看此特定请求路径是否会碰到已注册的控制器之一? We used
xxxxcontroller : ControllerBase
and a [Route("controller/{version}/{id}]
attribute to register the controller. 我们使用了
xxxxcontroller : ControllerBase
和[Route("controller/{version}/{id}]
属性来注册控制器。
Thanks a lot. 非常感谢。
我建议您看一下Asp.net核心身份,如果我了解您要查找的内容,则需要使用角色来保证对某些路由的访问。
I don't know how to get a list of all routes and check that the path is for a valid route but you can use middleware to check the response status code after MVC runs and if the status code is a 404 then you know it wasn't a valid route and you can handle it accordingly. 我不知道如何获取所有路由的列表并检查路径是否为有效路由,但是您可以在MVC运行后使用中间件来检查响应状态代码,并且如果状态代码是404,那么您知道它不是不是有效的路线,您可以相应地进行处理。
The UseStatusCodePagesWithReExecute
extension method basically uses this approach to handle not only 404 errors but all error status codes. UseStatusCodePagesWithReExecute
扩展方法基本上使用此方法不仅处理404错误,还处理所有错误状态代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.