问题描述
有没有办法让我赶上的所有传入的请求到我的ASP.NET MVC应用程序4及以后继续请求到指定的控制器/动作之前运行一些code?
Is there a way for me to catch all incoming requests to my ASP.NET MVC 4 app and run some code before continuing the request onward to the specified controller/action?
我需要运行一些自定义AUTH code。与现有的服务,并正确地做到这一点,我需要能够拦截来自所有客户端的所有传入的请求仔细检查一些事情与其他服务。
I need to run some custom auth code with existing services, and to do this properly, I'll need to be able intercept all incoming requests from all clients to double check some things with the other service.
推荐答案
最正确的方法是创建一个继承ActionFilterAttribute并覆盖 OnActionExecuting
方法。这可以然后在 GlobalFilters
在的Global.asax.cs
The most correct way would be to create a class that inherits ActionFilterAttribute and override OnActionExecuting
method. This can then be registered in the GlobalFilters
in Global.asax.cs
当然,这只会拦截,实际上有一个路由请求。
Of course, this will only intercept requests that actually have a route.
这篇关于ASP.NET MVC 4拦截所有的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!