如何将模型从单独的MVC项目传递到外部Web Api

时间:2014-12-22 08:07:26

标签: c# asp.net-web-api asp.net-mvc-5

我的Web API项目中有这个方法

 public UserProfile CheckCredentials([FromBody] UserLogin userLogin)
 {
        UserProfile userProfile;

        userLogin.Password = Crypto.Encrypt(userLogin.Password);

        return userProfile;
 }

在我单独的MVC项目中,我需要调用此方法并传入UserLogin模型,该模型从我的MVC项目中的视图中填充。

我知道如何在javascript中执行此操作,如此处所示(示例)

var apiurl = "http://localhost:50793/api/ImageUpload/UploadImage"

var customer = { customer_name: "Scott", company_name: "HP" };
$.ajax({
    type: "POST",
    data: JSON.stringify(customer),
    url: apiurl,
    contentType: "application/json"
});

将此网络API称为

 [HttpPost]
public void UploadImage([FromBody] Customer customer)
{
    string name = customer.company_name;

    string dooda = string.Empty;
}

但我不确定如何在MVC中执行此操作/语法我已经使用Google搜索无效的解决方案,有人可以向我展示如何执行此操作的解决方案。

由于

**更新** 我已经安装了RestSharp,现在我有以下代码

   var url = "http://localhost:28247/api/UserLoginApi/CheckCredentials";

            var client = new RestClient();
            var request = new RestRequest(Method.POST) { Resource = url, RequestFormat = DataFormat.Json };

            request.AddBody(userLogin);

            var response = (RestResponse)client.Execute(request);
            Console.WriteLine(response.Content);

返回错误     值不能为空。\ r \ n参数名称:uri“

*我的控制器*

[HttpPost]
    public ActionResult Login(UserLogin userLogin)
    {

      // Call Web Api here and pass in userLogin

       return View(userLogin);

     }

原来我上面展示的代码,即我不能使用的其他客户端,因为它是post get put和delete方法,因为我的api控制器有各种各样的post方法,不适合所以我有问一个问题我如何调用上面的api检查凭证在登录模型中传递

0 个答案:

没有答案