简体   繁体   English

如何在请求到达控制器之前验证URL

[英]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.

相关问题 使用ASP.NET MVC 5更改请求后,如何在控制器中手动验证模型? - How can I manually validate a model in the controller after altering the request using ASP.NET MVC 5? 如何在一个地方验证所有控制器的 Request.Headers[&quot;Authorization&quot;] ? - How can I validate Request.Headers["Authorization"] for all controller at a single place? 如何验证SOAP请求 - How can I validate a SOAP Request 如何从 Angular 请求 c# controller 中的参数? (没有 url 串联) - How can I request parameters in c# controller from Angular? (no url concatenation) 在控制器上添加值后如何使用ModelState进行验证? - How can I validate with ModelState after adding values on the controller? 我怎样才能看到发送的实际原始请求 - How can I the see actual raw request that gets sent 如何验证此方案的Web api请求对象? - how can I validate a web api request object for this scenario? 如何在 C# 中验证 URL 以避免 404 错误? - How can I validate a URL in C# to avoid 404 errors? 如何测试当前请求是否为Controller中的ajax请求? - How can I test if the current request is an ajax request in a Controller? MVC +如何在控制器操作之前提醒提示用户 - MVC + How can i alert prompt user before Controller action
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM