The difference between the <ViewResult> and ViewResult jobs

If I just returned the view, is there a performance difference to get it back from Task?

[HttpGet]
public Task<ViewResult> Index()
{
   return Task.FromResult(View());
}

[HttpGet]
public ViewResult Index()
{
   return View();
}
+5
source share
2 answers

In your case, the version Taskis likely to be slower because you are just adding overhead Taskwithout any benefits. Returning Taskmakes sense when you can use async- await, that is, if you are really doing some kind of action that can be made asynchronous in your method.

+3
source

, ; . , .

, Professional ASP.NET MVC 4

public class PortalController : Controller {
    public ActionResult Index(string city) {
        NewsService newsService = new NewsService();
        WeatherService weatherService = new WeatherService();
        SportsService sportsService = new SportsService();
        PortalViewModel model = new PortalViewModel {
            News = newsService.GetNews(city),
            Weather = weatherService.GetWeather(city),
            Sports = sportsService.GetScores(city)
        };
        return View(model);
    }
}

, , , , , . 200, 300 400 (), 900 ( ).

, :

public class PortalController : Controller {
    public async Task<ActionResult> Index(string city) {
        NewsService newsService = new NewsService();
        WeatherService weatherService = new WeatherService();
        SportsService sportsService = new SportsService();
        var newsTask = newsService.GetNewsAsync(city);
        var weatherTask = weatherService.GetWeatherAsync(city);
        var sportsTask = sportsService.GetScoresAsync(city);
        await Task.WhenAll(newsTask, weatherTask, sportsTask);
        PortalViewModel model = new PortalViewModel {
            News = newsTask.Result,
            Weather = weatherTask.Result,
            Sports = sportsTask.Result
        };
        return View(model);
    }
}

, , , , . 200, 300 400 , 400 ( ).

+1

All Articles