我正在尝试使用Microsoft Graph API,并在Microsoft开发人员网站上使用QuickStarts设置了一个项目。
我在尝试列出与已记录帐户关联的所有电子邮件联系人时遇到问题。
我的问题似乎在于我如何请求并将信息传递给ViewBag
,因为即使使用正确的前端列表,它也会抛出NullReference
个例外。
以下是相关代码:
HomeController.cs
public async Task<ActionResult> GetMyContacts()
{
try
{
// Init GraphServiceClient
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
var contacts = await graphService.GetMyEmailContacts(graphClient);
ViewBag.Contacts = contacts;
return View("Graph");
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = Resource.Error_Message + Request.RawUrl + ": " + se.Error.Message });
}
}
GraphService.cs
public async Task<IList<Contact>> GetMyEmailContacts(GraphServiceClient graphClient)
{
User me = await graphClient.Me.Request().Select("mail,userPrincipalName").GetAsync();
var contacts = me.Contacts;
return contacts;
}
Graph.cshtml
<div class="col-sm-12">
<pre name="user-contacts">@ViewBag.Contacts</pre>
</div>
<h2>TEST GET CONTACTS</h2>
<div class="col-sm-12">
<p>@Html.Raw(ViewBag.Contacts)</p>
</div>
我在这里缺少什么?
答案 0 :(得分:2)
在您的视图中,您必须将其强制转换为原始类型。 ViewBag是C#4动态类型。除非被投射,否则返回的实体也是动态的。
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics characteristics = mCameraCharacteristics;
int sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
如果要显示整个列表,就像这样简单:
var contactList = ViewBag.Contacts as IEnumerable<Contact>;