asp.net core middleware vs filters

后端 未结 3 1731
星月不相逢
星月不相逢 2020-12-02 12:57

After reading about asp.net core middlware, I am confused when should I use filters and when should I use middlewares as they seem to achieve the same goal. When should midd

3条回答
  •  孤街浪徒
    2020-12-02 13:16

    Middleware operate on the level of ASP.NET Core and can act on every single request that comes in to the application.

    MVC filters on the other hand only run for requests that come to MVC.

    So for example, if I wanted to enforce that all requests must be done over HTTPS, I would have to use a middleware for that. If I made an MVC filter that did that, users could still request e.g. static files over HTTP.

    But then on the other hand something that logs request durations in MVC controllers could absolutely be an action filter.

提交回复
热议问题