如何在MVC模型中使用Webservice

Rup*_*esh 0 c# asp.net-mvc wcf web-services

我有一个非常简单的MVC模型,其中我有两个非常简单的模型类Person和Company.

我必须使用Web服务来获取有关人员和公司的数据.

您能否发布一些示例linke,其中webservice被用于GET或/和POST.

这是我的控制器索引方法.

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

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

我不知道是否在Get或Post中写上面的代码.

Gia*_*los 5

我个人在模型中使用它.例如,我有一个OData服务,我在我的模型中调用它:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在控制器中:

public ActionResult Index(int Id)
{
    return View(new Person(Id));
}
Run Code Online (Sandbox Code Playgroud)