背景:我有这个学校项目,我想使用https://developer.edamam.com/edamam-docs-recipe制作食谱Web应用程序-api。
我到目前为止所做的:
- 在Postman中提出了一个请求,并获取JSON数据并使用JSON2CSHARP.com生成C#
namespace EdamaAPI.Models
{
public class Rootobject
{
public string q { get; set; }
public int from { get; set; }
public int to { get; set; }
public Params _params { get; set; }
public bool more { get; set; }
public int count { get; set; }
public Hit[] hits { get; set; }
}
public class Params
{
public object[] sane { get; set; }
public string[] q { get; set; }
public string[] app_key { get; set; }
public string[] health { get; set; }
public string[] from { get; set; }
public string[] to { get; set; }
public string[] calories { get; set; }
public string[] app_id { get; set; }
}
public class Hit
{
public Recipe recipe { get; set; }
public bool bookmarked { get; set; }
public bool bought { get; set; }
}
public class Recipe
{
public string uri { get; set; }
public string label { get; set; }
public string image { get; set; }
public string source { get; set; }
public string url { get; set; }
public string shareAs { get; set; }
public float yield { get; set; }
public string[] dietLabels { get; set; }
public string[] healthLabels { get; set; }
public object[] cautions { get; set; }
public string[] ingredientLines { get; set; }
public Ingredient[] ingredients { get; set; }
public float calories { get; set; }
public float totalWeight { get; set; }
public Totalnutrients totalNutrients { get; set; }
public Totaldaily totalDaily { get; set; }
public Digest[] digest { get; set; }
}
public class Totalnutrients
{
public ENERC_KCAL ENERC_KCAL { get; set; }
public FAT FAT { get; set; }
public FASAT FASAT { get; set; }
public FATRN FATRN { get; set; }
public FAMS FAMS { get; set; }
public FAPU FAPU { get; set; }
public CHOCDF CHOCDF { get; set; }
public SUGAR SUGAR { get; set; }
public PROCNT PROCNT { get; set; }
public CHOLE CHOLE { get; set; }
public NA NA { get; set; }
public CA CA { get; set; }
public MG MG { get; set; }
public K K { get; set; }
public FE FE { get; set; }
public ZN ZN { get; set; }
public P P { get; set; }
public VITA_RAE VITA_RAE { get; set; }
public VITC VITC { get; set; }
public THIA THIA { get; set; }
public RIBF RIBF { get; set; }
public NIA NIA { get; set; }
public VITB6A VITB6A { get; set; }
public FOLDFE FOLDFE { get; set; }
public FOLFD FOLFD { get; set; }
public VITB12 VITB12 { get; set; }
public VITD VITD { get; set; }
public TOCPHA TOCPHA { get; set; }
public VITK1 VITK1 { get; set; }
public FIBTG FIBTG { get; set; }
}
public class ENERC_KCAL
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FAT
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FASAT
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FATRN
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FAMS
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FAPU
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class CHOCDF
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class SUGAR
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class PROCNT
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class CHOLE
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class NA
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class CA
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class MG
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class K
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FE
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class ZN
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class P
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class VITA_RAE
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class VITC
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class THIA
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class RIBF
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class NIA
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class VITB6A
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FOLDFE
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FOLFD
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class VITB12
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class VITD
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class TOCPHA
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class VITK1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FIBTG
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class Totaldaily
{
public ENERC_KCAL1 ENERC_KCAL { get; set; }
public FAT1 FAT { get; set; }
public FASAT1 FASAT { get; set; }
public CHOCDF1 CHOCDF { get; set; }
public PROCNT1 PROCNT { get; set; }
public CHOLE1 CHOLE { get; set; }
public NA1 NA { get; set; }
public CA1 CA { get; set; }
public MG1 MG { get; set; }
public K1 K { get; set; }
public FE1 FE { get; set; }
public ZN1 ZN { get; set; }
public P1 P { get; set; }
public VITA_RAE1 VITA_RAE { get; set; }
public VITC1 VITC { get; set; }
public THIA1 THIA { get; set; }
public RIBF1 RIBF { get; set; }
public NIA1 NIA { get; set; }
public VITB6A1 VITB6A { get; set; }
public FOLDFE1 FOLDFE { get; set; }
public VITB121 VITB12 { get; set; }
public VITD1 VITD { get; set; }
public TOCPHA1 TOCPHA { get; set; }
public VITK11 VITK1 { get; set; }
public FIBTG1 FIBTG { get; set; }
}
public class ENERC_KCAL1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FAT1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FASAT1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class CHOCDF1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class PROCNT1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class CHOLE1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class NA1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class CA1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class MG1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class K1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FE1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class ZN1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class P1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class VITA_RAE1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class VITC1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class THIA1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class RIBF1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class NIA1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class VITB6A1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FOLDFE1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class VITB121
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class VITD1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class TOCPHA1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class VITK11
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class FIBTG1
{
public string label { get; set; }
public float quantity { get; set; }
public string unit { get; set; }
}
public class Ingredient
{
public string text { get; set; }
public float weight { get; set; }
}
public class Digest
{
public string label { get; set; }
public string tag { get; set; }
public string schemaOrgTag { get; set; }
public float total { get; set; }
public bool hasRDI { get; set; }
public float daily { get; set; }
public string unit { get; set; }
public Sub[] sub { get; set; }
}
public class Sub
{
public string label { get; set; }
public string tag { get; set; }
public string schemaOrgTag { get; set; }
public float total { get; set; }
public bool hasRDI { get; set; }
public float daily { get; set; }
public string unit { get; set; }
}
}
- 发送GET请求以访问数据并进行估算。
public class RecipeController : Controller
{
public async Task<ActionResult> Index()
{
List<Recipe> RecInfo = new List<Recipe>();
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri("https://api.edamam.com");
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Sending request to find web api REST service resource GetAllEmployees using HttpClient
HttpResponseMessage Res = await client.GetAsync("api.edamam.com/search?q=chicken&app_id=a88093f8&app_key=4513de36c431f9936462ef4391f631e4&from=0&to=3&calories=gte%20591,%20lte%20722&health=alcohol-free");
//Checking the response is successful or not which is sent using HttpClient
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var name = Res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the Employee list
RecInfo = JsonConvert.DeserializeObject<List<Recipe>>(name);
}
//returning the employee list to view
return View(RecInfo);
}
}
}
- 带有视觉工作室的自动化视图。
@model IEnumerable<EdamaAPI.Models.Recipe>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.uri)
</th>
<th>
@Html.DisplayNameFor(model => model.label)
</th>
<th>
@Html.DisplayNameFor(model => model.image)
</th>
<th>
@Html.DisplayNameFor(model => model.source)
</th>
<th>
@Html.DisplayNameFor(model => model.url)
</th>
<th>
@Html.DisplayNameFor(model => model.shareAs)
</th>
<th>
@Html.DisplayNameFor(model => model.yield)
</th>
<th>
@Html.DisplayNameFor(model => model.calories)
</th>
<th>
@Html.DisplayNameFor(model => model.totalWeight)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.uri)
</td>
<td>
@Html.DisplayFor(modelItem => item.label)
</td>
<td>
@Html.DisplayFor(modelItem => item.image)
</td>
<td>
@Html.DisplayFor(modelItem => item.source)
</td>
<td>
@Html.DisplayFor(modelItem => item.url)
</td>
<td>
@Html.DisplayFor(modelItem => item.shareAs)
</td>
<td>
@Html.DisplayFor(modelItem => item.yield)
</td>
<td>
@Html.DisplayFor(modelItem => item.calories)
</td>
<td>
@Html.DisplayFor(modelItem => item.totalWeight)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
当我尝试运行项目时,发生此错误:
传递到字典中的模型项是'edamaapi.models.recipe'的类型,但是该词典需要类型的模型项'system.collections.generic.eneric.ienumobles.1 [edamaapi.models.models.ingredient]'。<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
堆栈跟踪:
[invalidoperationException:传递到字典中的模型项是'edamaapi.models.recipe'的类型,但是该词典需要一个类型的模型项'system.collections.generic.generic.eneric.ienumoserth'1 [edamaapi.models.models.models.singredient]'。] system.web.mvc.viewdaticticary`1.setModel(对象值) 175 system.web.mvc.viewdatadicticationary..ctor(viewDatadictionary dictionary) 107 system.web.mvc.webviewpage`1.setViewData(viewDatadictionary ViewData) 49 system.web.mvc.razorview.renderview(ViewContext ViewContext,Textwriter Writer,Object实例) 99 system.web.mvc.buildmanagercompiledview.render(ViewContext ViewContext,Textwriter Writer) 107 system.web.mvc.viewresultbase.executeresult(controllerContext上下文) 291 system.web.mvc.controllerActionInvoker.invokeactionResult(contranceerContext ControllerContext,ActionResresult ActionResult) 13 system.web.mvc.controllerActionInVoker.invokeactionResultFilterRecursiverVorsiver(IList`1滤波器,INT32 FilterIndex,resultexecutingContextProvontext,contranceErcontext contranceptext,contranceerContext contranceerContext,Action Resresult ActionResult) 56) 56 system.web.mvc.controllerActionInvoker.invokeActionResultFilterRecursiveriver(Ilist'1滤波器,INT32 FilterIndex,resultExeCutingContextProvontext,contranceerContext contranceptext,contranceerContext contranceercontext,ActionRessult action RestionResult) 420) 420) 420 system.web.mvc.controllerActionInVoker.invokeactionResultWithFilters(contranceerContext ControllEctext,Ilist'1滤波器,ActionResult ActionResult) 52 system.web.mvc.async.c__displayclass2b.b__1c() 173 system.web.mvc.async.c__displayclass21.b__1e(iasyncresult asyncresult) 100 system.web.mvc.Async.WrappedAsyncresult`1.CallendDelegate(iasyncresult asyncresult) 10 system.web.mvc.async.wrappedasyncresultbase`1.end() 49 system.web.mvc.async.asynccontrollerActionInVoker.EndinVokeaction(iasyncresult asyncresult) 27 system.web.mvc.controller.b__1d(iasyncresult asyncresult,executecorestate innerstate) 13 system.web.mvc.Async.wrappedAsyncVoid`1.CallendDelegate(iasyncresult asyncresult) 29 system.web.mvc.async.wrappedasyncresultbase`1.end() 49 system.web.mvc.controller.endexecutecore(iasyncresult asyncresult) 36 system.web.mvc.controller.b__15(iasyncresult asyncresult,控制器控制器) 12 system.web.mvc.async.warpapedasyncvoid`1.callendDelegate(iasyncresult asyncresult) 22 system.web.mvc.async.wrappedasyncresultbase`1.end() 49 system.web.mvc.controller.endexecute(iasyncresult asyncresult) 26 system.web.mvc.controller.system.web.mvc.async.iasynccontroller.endexecute(iasyncresult asyncresult) 10 system.web.mvc.mvchandler.b__5(iasyncresult asyncresult,processRequeststate innerstate) 21 system.web.mvc.Async.wrappedAsyncVoid`1.CallendDelegate(iasyncresult asyncresult) 29 system.web.mvc.async.wrappedasyncresultbase`1.end() 49 system.web.mvc.mvchandler.endprocessrequest(iasyncresult asyncresult) 28 system.web.mvc.mvchandler.system.web.ihttpasynchandler.endprocessrequest(iasyncresult结果) 9 system.web.callhandlerexecutionstep.onasynchandlecompletion(iasyncresult ar) 129
我一直在Google上搜索几个小时,没有运气,我现在可以真正使用指导手!如果我忘记了任何信息,请告诉我,我会立即解决这些信息。
例外说明了一切:
查看异常
传递到字典中的模型项是'edamaapi.models.recipe'的类型,但是该词典需要一个类型的模型项'system.collections.generic.generic.eneric.ienumoserth'1 [edamaapi.models.models.ingredient]'。
查看模型:
@model IEnumerable<EdamaAPI.Models.Recipe>
当视图所需的成分模型时,您将食谱模型传递给视图。
这将有效:
public class RecipeController : Controller
{
public async Task<ActionResult> Index()
{
List<Ingredient> RecInfo = new List<Ingredient>();
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri("https://api.edamam.com");
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Sending request to find web api REST service resource GetAllEmployees using HttpClient
HttpResponseMessage Res = await client.GetAsync("api.edamam.com/search?q=chicken&app_id=a88093f8&app_key=4513de36c431f9936462ef4391f631e4&from=0&to=3&calories=gte%20591,%20lte%20722&health=alcohol-free");
//Checking the response is successful or not which is sent using HttpClient
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var name = Res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the Employee list
RecInfo = JsonConvert.DeserializeObject<List<Ingredient>>(name);
}
//returning the employee list to view
return View(RecInfo);
}
}
}
,但这不是您需要的,因为您需要配方模型。更改您的操作以接受通用食谱列表。