2

I'm from Java background, but have been given a ASP.NET MVC RESTful services project to work on. I need some guidance as to where to start.

I have a web service running on the server that returns JSON data. I've to build a MVC system to consume the data including user login, account creation, and basic CRUD operations.

I'm confused about how to connect the model to the web service?

How can I connect to it using MVC? DO I have to use Web API? I've watched few tutorials, but to no avail so far. Any suggestion for the tutorials?

1 Answer 1

2

I think this tutorial will do what you are looking for

http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

with web apis you have a few options. the above sample is a simple way of doing it.

you can also use Angular JS or knockoutjs or my favorite one is using breeze js.

Adding more information about how to connect model to the web service

in the above example, in thr Adding a model section:

Class product is being created :

    public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
    public decimal Price { get; set; }
}

this class is the blue print of our model

we fill this model in the Controller ( Controller is responsible for managing our model and introduce it to the view)

So product class being filled here

 Product[] products = new Product[] 
    { 
        new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
        new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
        new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
    };

The main point is our controller should be inherited from ApiController.

 public IEnumerable<Product> GetAllProducts()
    {
        return products;
    }

    public IHttpActionResult GetProduct(int id)
    {
        var product = products.FirstOrDefault((p) => p.Id == id);
        if (product == null)
        {
            return NotFound();
        }
        return Ok(product);
    }

The above two methods are getting data from our model and returning data.

and at the end our javascript file is responsible for reading the data by using $.getJSON .

so to answer your question. how connecting model to web services. the answer is in the methods in controllers.

this is a very simple sample. in real-world data needs to be read from database with using entity framework

this sample shows how to do it

http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply, appreciated. It misses though on the main culprit for me, connecting model to the web service or how to consume that web service in MVC.
I tried to expand my answer although this is a very broad area. let me know if you have any problem with your application. send us more detailed problem we will be happy to help. I suggest start developing and on each piece of work where you have problem send your code, the guide will come to you
Thanks a lot for the detailed answer. I'll comeback once I hit a roadblock. A quick question though, in the example, products are getting created in the code like 'new Product { Id = 1, ....', in my case, all data is coming from JSON web service. When I tried instantiating web service in the controller, at runtime I get a runtime error for 'Could not find default endpoint element that references contract' ... any ideas?

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.