2

I'm trying to learn about web services. The US government has several web services that can be consumed so this could be an ideal place to start. For example, here's one that provides Fuel Economy information http://www.fueleconomy.gov/feg/ws/index.shtml.

If I'd like to display basic information in a view (using ASP.NET MVC) such as current fuel prices (/ws/rest/fuelprices) how/where can I get started? It seems to be so many ways to do this (WCF? SOAP? REST? Maybe I'm way off base??) and I just need a basic get starting documentation so I can get my feet wet.

1 Answer 1

4

You could use the new HttpClient class that is built into .NET 4.0 to consume RESTful web services. For example let's suppose that you wanted to return the current fuel prices (http://www.fueleconomy.gov/ws/rest/fuelprices).

Start by designing a model that will map the XML that this service is returning:

[DataContract(Name = "fuelPrices", Namespace = "")]
public class FuelPrices
{
    [DataMember(Name = "cng")]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal Cng { get; set; }

    [DataMember(Name = "diesel")]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal Diesel { get; set; }

    [DataMember(Name = "e85")]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal E85 { get; set; }

    [DataMember(Name = "electric")]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal Electric { get; set; }

    [DataMember(Name = "lpg")]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal Lpg { get; set; }

    [DataMember(Name = "midgrade")]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal MidGrade { get; set; }

    [DataMember(Name = "premium")]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal Premium { get; set; }

    [DataMember(Name = "regular")]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal Regular { get; set; }
}

then you could have a controller that will query the remote REST service and bind the result to the model:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        using (var client = new HttpClient())
        {
            var url = "http://www.fueleconomy.gov/ws/rest/fuelprices";
            var response = client.GetAsync(url).Result;
            response.EnsureSuccessStatusCode();
            FuelPrices fuelPrices = response.Content.ReadAsAsync<FuelPrices>().Result;
            return View(fuelPrices);
        }
    }
}

and finally a view to display the results:

@model FuelPrices

<table>
    <thead>
        <tr>
            <th>CNG</th>
            <th>Diesel</th>
            <th>E 85</th>
            <th>Electric</th>
            <th>LPG</th>
            <th>Mid grade</th>
            <th>Premium</th>
            <th>Regular</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>@Html.DisplayFor(x => x.Cng)</td>
            <td>@Html.DisplayFor(x => x.Diesel)</td>
            <td>@Html.DisplayFor(x => x.E85)</td>
            <td>@Html.DisplayFor(x => x.Electric)</td>
            <td>@Html.DisplayFor(x => x.Lpg)</td>
            <td>@Html.DisplayFor(x => x.MidGrade)</td>
            <td>@Html.DisplayFor(x => x.Premium)</td>
            <td>@Html.DisplayFor(x => x.Regular)</td>
        </tr>
    </tbody>
</table>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this. It really helped me understand how web services can work with MVC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.