我怎样才能调用从MVC控制器的操作方法一的WebAPI的方法? [英] How can i call a WebApi method from MVC Controller Action Method?

查看:280
本文介绍了我怎样才能调用从MVC控制器的操作方法一的WebAPI的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以调用从MVC的动作控制器方法的网络API方法。这就是我想,这是我的控制器的方法:

How can i call an Web API method from an MVC action controller method. This is what i am trying, this is my controller method:

public ActionResult ProductDetail(long id)
{
    using (var client = new HttpClient())
    {
        var productDetailUrl = Url.RouteUrl(
            "DefaultApi",
            new { httproute = "", controller = "ProductDetails", id = id },
            Request.Url.Scheme
        );
        var model = client
                    .GetAsync(productDetailUrl)
                    .Result
                    .Content.ReadAsAsync<ProductItems>().Result;

        return View(model);
    }
}

我的Web API方法:

My Web API method:

private ProductEntities products = new ProductEntities();

public IEnumerable<ProductItems> GetProductDetail(int ID)
{            
   var produc= products.ExecuteStoreQuery<ProductItems>(
            "GetProductDetail @ProductID ", 
            new SqlParameter("@ProductID", ID)); 

   return produc;
}

当我返回数据后说,这样我在我的MVC的操作方法得到一个错误@ VAR模型

When i am doing this i am getting an error @ var model in my MVC action method after returning the data saying

Newtonsoft.Json.JsonSerializationException:无法反序列化JSON
  阵(即[1,2,3])入式ProductDetails.Models.ProductItems。
  反序列化的类型必须是一个数组或实现一个集合
  接口像IEnumerable的,ICollection的或IList的。要强制JSON阵列
  反序列化添加JsonArrayAttribute的类型。 1号线,
  位置1。

"Newtonsoft.Json.JsonSerializationException: Cannot deserialize JSON array "(i.e. [1,2,3]) into type 'ProductDetails.Models.ProductItems'. The deserialized type must be an array or implement a collection interface like IEnumerable, ICollection or IList. To force JSON arrays to deserialize add the JsonArrayAttribute to the type. Line 1, position 1."

任何一个可以帮我做这个......还是建议我任何其他的最好的方法或纠正我,如果我做错了任何地方......我必须展示在我看来,返回的数据......数据应该是回到我的控制器

Can any one help me doing this...or suggest me any other best method or correct me if I am doing wrong any where... I have to show the returned data in my view... the data should be returned to my controller

推荐答案

作为例外规定不能反序列化 JSON数组......你在接收端例如使用反序列化不是问题与seliazation您的Web API方法。

As the exception states "Cannot deserialize JSON array ..." you have a problem on the receiver side e.g with the deserialization not in your web api method with the seliazation.

和你的问题是,你试图反序列化到错误类型becaues您的Web API方法如下:

And your problem is that you try to deserialize into the wrong type becaues your web api method looks like:

public IEnumerable<ProductItems> GetProductDetail(int ID)

所以它返回的IEnumerable&LT; ProductItems&GT; (例如数组 ProductItems ),但在你的控制器动作,你试试反序列化响应至有一个 ProductItems 与呼叫 ReadAsAsync&LT; ProductItems&GT;()

So it returns IEnumerable<ProductItems> (e.g. an array of ProductItems) but in your controller action you try to deserialize the response into one ProductItems with the call ReadAsAsync<ProductItems>()

因此​​,改变你的 ReadAsAsync 反序列化到一个数组 ProductItems ,它应该工作:

So change your ReadAsAsync to deserialize into an array ProductItems and it should work:

var model = client
               .GetAsync(productDetailUrl)
               .Result
               .Content.ReadAsAsync<ProductItems[]>().Result;

这篇关于我怎样才能调用从MVC控制器的操作方法一的WebAPI的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆