C# 在Microsft图形中调用和显示成员列表的结果
我试图在我的C# 在Microsft图形中调用和显示成员列表的结果,c#,asp.net-mvc,microsoft-graph-api,C#,Asp.net Mvc,Microsoft Graph Api,我试图在我的ASP.NET MVC应用程序中显示用户所属的Office 365组。在我的userscoontroller中,我创建了一个Microsoft Graph客户端,并请求Graph服务调用MemberOf API。 问题是,当我想在视图中显示来自控制器的结果时,我不知道应该在我的模型中调用哪个Microsoft Graph命名空间来显示组名、描述以及调用:get时得到的其他响应 我遵循了有关列表memberOf()的Microsoft Graph文档,并在我的UsersControll
ASP.NET MVC
应用程序中显示用户所属的Office 365组。在我的userscoontroller
中,我创建了一个Microsoft Graph客户端,并请求Graph服务调用MemberOf API。
问题是,当我想在视图中显示来自控制器的结果时,我不知道应该在我的模型中调用哪个Microsoft Graph命名空间来显示组名、描述以及调用:get时得到的其他响应
我遵循了有关列表memberOf()的Microsoft Graph文档,并在我的UsersController中包含了以下代码:
public async Task<ActionResult> MyGroups()
{
string token = await GetAccessToken();
if (string.IsNullOrEmpty(token))
{
// If there's no token in the session, redirect to Home
return Redirect("/");
}
GraphServiceClient client = new GraphServiceClient(
new DelegateAuthenticationProvider(
(requestMessage) =>
{
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", token);
return Task.FromResult(0);
}));
try
{
var memberOf = await client.Me.MemberOf.Request()
.GetAsync();
return View(memberOf);
}
catch (ServiceException ex)
{
return RedirectToAction("Error", "Home", new { message = "ERROR retrieving events", debug = ex.Message });
}
}
var memberOf = await client.Me.MemberOf.Request().GetAsync();
List<Group> groups = memberOf.OfType<Microsoft.Graph.Group>().ToList();
return View(groups);
公共异步任务MyGroups()
{
字符串令牌=等待GetAccessToken();
if(string.IsNullOrEmpty(令牌))
{
//如果会话中没有令牌,请重定向到主页
返回重定向(“/”);
}
GraphServiceClient=新GraphServiceClient(
新的DelegateAuthenticationProvider(
(请求消息)=>
{
requestMessage.Headers.Authorization=
新的AuthenticationHeaderValue(“承载者”,令牌);
返回Task.FromResult(0);
}));
尝试
{
var memberOf=await client.Me.memberOf.Request()
.GetAsync();
返回视图(memberOf);
}
捕获(ServiceException ex)
{
返回RedirectToAction(“Error”,“Home”,new{message=“Error retrieving events”,debug=ex.message});
}
}
然后,在MyGroups视图中,我正在访问Microsoft.Graph.Group命名空间,但在运行项目时出错
这是视图代码:
@model Microsoft.Graph.Group
@{
ViewBag.Title = "My info";
}
<h2>My groups</h2>
<table class="table">
<tr>
<th>
DisplayName
</th>
<th>
GroupTypes
</th>
</tr>
<tr>
<td>
@this.Model.DisplayName
</td>
<td>
@this.Model.Description
</td>
</tr>
@model Microsoft.Graph.Group
@{
ViewBag.Title=“我的信息”;
}
我的团队
显示名称
组类型
@this.Model.DisplayName
@这是一个模型描述
这是我在浏览器上遇到的错误:
传递到字典中的模型项的类型为“Microsoft.Graph.UserMemberOfCollectionWithReferencesPage”,但此字典需要类型为“Microsoft.Graph.Group”的模型项
有人能告诉我应该使用哪个正确的Microsoft Graph命名空间来显示me/memberOf的结果吗?您打算显示所有组吗?如果是这样,您的模型可能应该是
IEnumerable
。以及循环中包含的
标记
@foreach (var item in Model)
{
<tr>
<td>
@item.displayName
</td>
<td>
@item.description
</td>
</tr>
}
我用UsersController中的以下代码显示了用户所属的组:
public async Task<ActionResult> MyGroups()
{
string token = await GetAccessToken();
if (string.IsNullOrEmpty(token))
{
// If there's no token in the session, redirect to Home
return Redirect("/");
}
GraphServiceClient client = new GraphServiceClient(
new DelegateAuthenticationProvider(
(requestMessage) =>
{
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", token);
return Task.FromResult(0);
}));
try
{
var memberOf = await client.Me.MemberOf.Request()
.GetAsync();
return View(memberOf);
}
catch (ServiceException ex)
{
return RedirectToAction("Error", "Home", new { message = "ERROR retrieving events", debug = ex.Message });
}
}
var memberOf = await client.Me.MemberOf.Request().GetAsync();
List<Group> groups = memberOf.OfType<Microsoft.Graph.Group>().ToList();
return View(groups);
var memberOf=wait client.Me.memberOf.Request().GetAsync();
列表组=memberOf.OfType().ToList();
返回视图(组);