2

I have a question about MVC asp.net I have a link that provides a data using HTTP in this XML format

 [...]
    <Item>
    <Name>Money</Name>
    <Unit>1000</Unit>
    </Item>
 [...]

If I want this to display data on view in my application - what should I use? WebAPI? The data ( < unit > ) change every few minutes, so always view have to display the current data.

Data are also possible to get in JSON format.

DiPix
  • 5,755
  • 15
  • 61
  • 108
  • It is really unclear what you're asking. Do you mean you have an external URL that you can call to provide this data in either XML or JSON format? And that you wish to display this data on a page in your web application? Then you don't need WebAPI at all; just perform an HTTP request from your controller, parse the data and set the appropriate model properties – CodeCaster Dec 14 '15 at 11:27
  • Exactly. This external URL provide data in XML and JSON. In that way: www.some-example-url/format=xml OR www.some-example-url/format=json – DiPix Dec 14 '15 at 11:32
  • So, what's your question? – CodeCaster Dec 14 '15 at 11:32
  • I want to display the data in my application. Eg. In some table. – DiPix Dec 14 '15 at 11:33
  • Just like you display any other kind of data in MVC: fill a model in your action method, render the model to HTML in your view. – CodeCaster Dec 14 '15 at 11:34
  • I don't get it. How am I supposed to get the data from that URL to my model? – DiPix Dec 14 '15 at 11:40
  • What you want to do is perform an HTTP request to the URL, and if you search this site for that you will get plenty of results. Example: [HTTP GET request and XML answer](http://stackoverflow.com/questions/4169982/http-get-request-and-xml-answer). – CodeCaster Dec 14 '15 at 11:40

5 Answers5

2

To clear up the confusion from all existing answers and comments: your actual problem statement is this:

I have a third-party URL that when requested, gives you some JSON which I wish to display in a table on an MVC view.

This is very trivial. See Deserializing JSON into an object to generate classes to deserialize the JSON. This provides you with a statically typed class that you can use from code.

Then you define a view model to hold a list of items:

public class JsonViewModel
{
    public List<JsonItem> Items { get; set; }
}

public class JsonItem
{
    public string Name { get; set; }
    public string Unit { get; set; }
}

And in the controller you perform an HTTP GET request to retrieve the JSON (HTTP GET request and XML answer), parse it, map it to your view model and return it to your view:

public class FooController : Controller
{
    public ActionResult Index()
    {
        // 1. Perform HTTP request to retrieve the JSON.
        var webClient = new WebClient();
        string rawJson = webClient.DownloadString("json-url");

        // 2. Parse the JSON.
        var jsonRootObject = JsonConvert.DeserializeObject<JsonRootObject>(rawJson);

        // 3. Map to your viewmodel
        var viewModel = new JsonViewModel
        {
            Items = jsonRootObject.Items.Select(i => new JsonItem
            {
                Name = i.Name,
                Unit = i.Unit
            }).ToList()
        };

        // 4. Return the model to your view
        return View(viewModel);
    }
}

Then finally you render the model in your view:

@model JsonViewModel

<table>
    <tr>
        <th>Name</th><th>Unit</th></tr>
    </tr>
@foreach (var item in Model.Items)
{
    <tr>
        <td>@item.Name</td><td>@item.Unit</td>
    </tr>
}
</table>
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Thank you, can you tell me what should i change if I want to display data in asynchronous mode? At the moment i've to refresh manual the page (and the data changes every few seconds). – DiPix Dec 14 '15 at 13:34
0

In your controller, call the external service to get the data. Can be XML, but JSON is more lightweight; I'd go for that. Parse the data into a view model that you then pass to the view. The view model contains the parsed information in the format that is best suited for your view; this will make sure your view can be kept as simple as possible (focusing on view logic).

To keep your controllers light, you might want to decide to move the retrieval and parsing logic in dedicated components, and use those from within your controller.

L-Four
  • 13,345
  • 9
  • 65
  • 109
  • It sounds nice, but how to do this? - im beginner – DiPix Dec 14 '15 at 11:34
  • @Piter see [Getting Started with ASP.NET MVC 5](http://www.asp.net/mvc/overview/getting-started/introduction/getting-started). – CodeCaster Dec 14 '15 at 11:35
  • There is anything about: "call the external service to get the data"...? – DiPix Dec 14 '15 at 11:38
  • As CodeCaster mentioned, start learning ASP.NET MVC first, as these are really the basic building blocks you should know about. – L-Four Dec 14 '15 at 11:38
  • What external service is it, exactly? – L-Four Dec 14 '15 at 11:39
  • @Piter If anyone is advising you to use a whole big round trip like Controller>ViewModel>View, they have not understood your requirement. This is very basic, you can parse this json from the view very easily by using jquery. – Kosala W Dec 14 '15 at 12:04
  • 1
    @Kosala: I would advice you to first learn the principles of MVC before downvoting a valid answer :) – L-Four Dec 14 '15 at 12:11
  • @Piter: to call a web api, look at http://stackoverflow.com/questions/19448690/how-to-consume-a-webapi-from-asp-net-web-api-to-store-result-in-database – L-Four Dec 14 '15 at 12:13
  • @L-Three I can also suggest you to learn the basics of software architecture before mastering any design pattern. :) – Kosala W Dec 14 '15 at 12:14
  • @Piter: as said, first learn the fundamentals of MVC. In the controller you build a view model based on the data that you receive from the external service; you then pass that view model to the view; and in the view you build your UI using the passed view model. It's quite simple actually. PS - don't put non-UI logic in the view as someone stated, it's bad practice. – L-Four Dec 14 '15 at 12:21
  • You need a tutorial about 'consuming REST API from C#'. For example: http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client – L-Four Dec 14 '15 at 12:35
0

Basically, what to use - JSON or View - is fully up to you and depends on your knowledges.

You can reach what you need here using JSON(or even XML) and js code in the page, or partial view and less js code.

I'd suggest you to return partial view from your MVC controller and update whole block.

Alex Lebedev
  • 601
  • 5
  • 14
-1

I prefer Json instead of Xml because it is lightweight, so... you can go through three different ways:

  1. Create a method that returns a JsonResult in your Controller (that is what i think you are doing) inside your asp.net mvc application;
  2. Create a WebApiController inside your asp.net mvc application just to have the web api stuff without creating a new application;
  3. Create a Web Api application and return your data (if you need only one service, i don't think you should create a new app for that, it is too much work)

If you choose the third option and you are not familliar with Web Api, i will recommend you to read this.

  • Don't spam your blog. You also aren't making a very compelling case; why would you introduce WebAPI just to return JSON, because _you prefer it_? Can't MVC return JSON? – CodeCaster Dec 14 '15 at 11:27
  • Just trying to help buddy.. I sad MVC could return a json at item 1, i don't know if you've seen – Nilson Junior Dec 14 '15 at 11:58
-1

You can simply use a periodic ajax call using setInterval to the 3rd party endpoint and then update your view accordingly. A better approach would be to use MVVM so you just need to update the view model and the framework will do the rest.

If you want to decouple your site from the 3rd party or you just want to hide that 3rd party address, You can use Web Api to call the 3rd party endpoint and call the web api endpoint from the client. You will need to handle CORS this way, if the web api is in a different domain.

You can also use mvc action instead of the web api but if you're planning on building large scale application. It's better that you start using SOA Architecture.

TGN12
  • 123
  • 1
  • 8