Making an asynchronous call in an MVC 5 action filter

Problem

I have a GET api method to retrieve a site configuration. For this, I use the GetAsync() and GetAsync() .

 HttpResponseMessage response = await client.GetAsync("api/{0}/config", id); 

Since I need this configuration on the site, I planned to use a global action filter for this.

Question

How can I call the async method in an MVC action filter? Or is there a better solution to this problem?

I looked at a few SO ( Asynchronous Action Filter in MVC 4 ) questions, but I did not find a satisfactory solution.

+7
asp.net-mvc async-await
source share
1 answer

It is not possible (reliable) to call an asynchronous method from an ASP.NET MVC 5 action filter. This has already been fixed in ASP.NET vNext, but AFAIK does not plan to support this in MVC 5.

If you absolutely must do this in the action filter, then you should use synchronous calls (e.g. WebClient instead of HttpClient ).

+12
source share

All Articles