C# Microsoft Graph API ASP.NET MVC获取驱动器项
我正在使用MicrosoftGraphAPI,我正在尝试获取文件夹中的驱动器项,这是我到目前为止得到的,我从中获得了大部分代码 我得到的驱动器项目如下:C# Microsoft Graph API ASP.NET MVC获取驱动器项,c#,asp.net-mvc,microsoft-graph-api,microsoft-graph-sdks,C#,Asp.net Mvc,Microsoft Graph Api,Microsoft Graph Sdks,我正在使用MicrosoftGraphAPI,我正在尝试获取文件夹中的驱动器项,这是我到目前为止得到的,我从中获得了大部分代码 我得到的驱动器项目如下: public static async Task<IEnumerable<DriveItem>> GetFilesAsync() { var graphClient = GetAuthenticatedClient(); var files = await graphClient.Me.Drive.Req
public static async Task<IEnumerable<DriveItem>> GetFilesAsync()
{
var graphClient = GetAuthenticatedClient();
var files = await graphClient.Me.Drive.Request().GetAsync();
return files.Items.CurrentPage;
}
我试图完成的是从OneDrive中获取特定文件夹中的文件。您的代码只是请求: MicrosoftGraph(实际上,一般来说RESTAPI)是非常文字化的,它们只返回您所请求的内容。因此,如果您请求一个容器(
Drive
),它不会自动返回该容器中的对象(DriveItem
)
为了接收DriveItems
的集合,您需要请求一个文件夹及其子文件夹。例如,您需要:
await graphClient
.Me
.Drive // Your default drive
.Root // The root of that drive
.Children // The children of "root"
.Request() // Build the request
.GetAsync(); // Execute the request
哪行代码会抛出错误?为了确定哪一段数据不应该为空,您做了哪些调试工作?这就是我现在要找出的,在我的本地主机上调试这个项目非常困难,因为我必须登录,您的堆栈跟踪至少应该告诉您这一行。为什么登录会使调试变得困难?根据堆栈,错误发生在这里:var files=await-graphClient.Me.Drive.Request().GetAsync();好啊但从理论上讲,
graphClient
、Me
、Drive
,或者Request()
的结果都可能是有问题的空值。您需要运行调试器并暂停错误,然后检查对象以查看空值,或者需要将其拆分为单独的行以将每个属性分配给单独的变量,然后当它第一次遇到空对象时,它将在确切的行上中断。您还没有说明为什么登录会使调试变得困难。
@model IEnumerable<Microsoft.Graph.DriveItem>
@{
ViewBag.Current = "Drive";
}
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Name</td>
</tr>
}
</tbody>
</table>
var files = await graphClient.Me.Drive.Request().GetAsync();
await graphClient
.Me
.Drive
.Request()
.GetAsync();
await graphClient
.Me
.Drive // Your default drive
.Root // The root of that drive
.Children // The children of "root"
.Request() // Build the request
.GetAsync(); // Execute the request