简体   繁体   中英

How to consume Webservice in MVC Model

I have very simple MVC model in which i have two very simple Model classes Person and Company.

I have to cousume a Webservice to get Data about person and Company.

Can you please post some example linke where webservice is consumed to GET or/and POST.

Here is my controller index method.

public ActionResult Index(string id)
{
Webservice webservice = new Webservice();
}

[HttpPost]
public ActionResult Index(string id)
{
Webservice webservice = new Webservice();
}

I dont know whether to write above code in Get or Post.

Personally i use it in the Model. For instance i have an OData service and i call it within my model:

public class Person
{
    public string Name {get;set;}
    public Person(int Id)
    {
        var oDataService = new ODataService(new Uri("YourURL"));
        Name = oDataService.Persons.Where(x=>x.Id == Id).Select(x=>x.Name);
    }
}

Then in the controller:

public ActionResult Index(int Id)
{
    return View(new Person(Id));
}

Model is your data. Controller will manage it. That means you should write the logic to load data in controller and convert it to your models' objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM