Channels ▼


Using Asynchronous Methods in ASP.NET 4.5 and in MVC 4

A simple example of a asynchronous ASP.NET MVC 4 action methods

With this knowledge of how to use asynchronous methods, I'll go back to ASP.NET MVC 4. I'll have the previously created MathService class use asynchronous execution in an ASP.NET MVC 4 app with a Razor view. To keep things simple, the sample solution has a simple structure and I don't create an additional project for the service. So, forgive me for using the MathService class without dependency injection. Don't consider direct instantiation the best-practice for ASP.NET MVC 4. Figure 5 shows the detailed structure for the Mvc4AsyncAwaitSample solution:

  • Controllers
    • MathController.cs
  • Models
    • Math
      • DetailsModel.cs
  • Views
    • Math
      • TotalSummary.cshtml
  • MathService.cs

Figure 5. The Mvc4AsyncAwaitSample solution with an ASP.NET MVC 4 example that uses asynchronous methods.

Here, I want to use two methods that were declared private in my previous example. The following lines show the two methods that are now public in the MathService class:

public static async Task<decimal> GetSubTotalAsync()
    var downloadedString = 
    await new WebClient().DownloadStringTaskAsync(new  Uri(""));

    return (decimal)downloadedString[0];

public static async Task<decimal> GetOtherChargesAsync()
    var downloadedString = 
    await new WebClient().DownloadStringTaskAsync(new Uri(""));

    return (decimal)downloadedString[1];

I've included DetailsModel in the Mvc4AsyncAwaitSample.Models.Math namespace. The following listing shows the code for DetailsModel.cs with just two properties:

namespace Mvc4AsyncAwaitSample.Models.Math
    public class DetailsModel
        public decimal SubTotal { get; set; }

        public decimal OtherCharges { get; set; }

The following code shows the code for the TotalSummary.cshtml view that uses the Razor engine. The view just displays the values for SubTotal and OtherCharges:

@model Mvc4AsyncAwaitSample.Models.Math.DetailsModel

    ViewBag.Title = "Total Summary (Async Sample)";

<h3>Total Summary</h3>

    <legend>Total Summary</legend>
    <div class="display-label">
        @Html.DisplayNameFor(model => model.SubTotal)
    <div class="display-field">
        @Html.DisplayFor(model => model.SubTotal)

    <div class="display-label">
        @Html.DisplayNameFor(model => model.OtherCharges)
    <div class="display-field">
        @Html.DisplayFor(model => model.OtherCharges)

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.