D2L API: 创建一个新部分
D2L API: Creating a new section
我正在构建一个接受 orgunitid 并创建与 orgunitid 关联的新部分的应用程序。我正在使用 c#。
这是我的代码。
string orgUnitId = textBoxOrgUnitId.Text;
string sectionCreateRoute = "/d2l/api/lp/1.0/" + orgUnitId + "/sections/";
var client = new RestClient(host);
var valenceAuthenticator = new D2L.Extensibility.AuthSdk.Restsharp.ValenceAuthenticator(userContext);
var requestCreateSection = new RestRequest(sectionCreateRoute, Method.POST);
valenceAuthenticator.Authenticate(client, requestCreateSection);
而且,我应该提供的 JSON 数据将如下所示。
{
"Name": "Test Section",
"Code": "" ,
"Description": { "Content": "Test", "Type" : "HTML" }
}
如何使用此 JSON 数据创建新部分。
谢谢,
菲利普
我试过这段代码,但它仍然没有创建一个部分。
string orgUnitId = textBoxOrgUnitId.Text;
string sectionCreateRoute = "/d2l/api/lp/1.0/" + orgUnitId + "/sections/";
var client = new RestClient(host);
var valenceAuthenticator = new D2L.Extensibility.AuthSdk.Restsharp.ValenceAuthenticator(userContext);
var requestCreateSection = new RestRequest(sectionCreateRoute, Method.POST);
requestCreateSection.AddJsonBody(new
{
Name = "Section Test",
Code = "156156",
Description = new { Content = "Test", Type = "Html" }
});
valenceAuthenticator.Authenticate(client, requestCreateSection);
因为您使用的是 ResSharp,所以我建议调用以下方法;
public IRestRequest AddJsonBody(object obj)
{
this.RequestFormat = DataFormat.Json;
return this.AddBody(obj, "");
}
但是,为了使用它,您需要一个 C# 对象来表示该数据。 class 定义看起来像这样;
public class NewSectionPostBody
{
public string Name;
public string Code;
public SectionContentType Description;
}
public class SectionContentType
{
public string Content;
public string Type;
}
有了这些和您现有的代码,我可以执行以下操作;
var requestCreateSection = new RestRequest(sectionCreateRoute, Method.POST);
// use object initializer to make instance for body inline
requestCreateSection.AddJsonBody(new NewSectionPostBody{
Name="Test Section",
Code=String.Empty,
Description= new SectionContentType { Content="Test", Type="HTLM" }
});
valenceAuthenticator.Authenticate(client, requestCreateSection);
RestSharp 将对象处理为 json 字符串序列化,因此您基本上可以将任何对象传递给该方法,生成的 json 将用作 post 主体。
最后一件事;如果这是一次性请求,您甚至不需要定义我用于正文的类型。如果您只是删除 classnames ie;
,则可以使用相同的内联初始化构造来创建匿名类型
requestCreateSection.AddJsonBody(new {
Name="Test Section",
Code=String.Empty,
Description= new { Content="Test", Type="HTLM" }
});
^^ 我没有实例化用户定义的 class 类型,而是使用匿名类型。如果您不能重复使用构成 post 正文的类型,这是一种更有效的设置请求的方法。
我正在构建一个接受 orgunitid 并创建与 orgunitid 关联的新部分的应用程序。我正在使用 c#。
这是我的代码。
string orgUnitId = textBoxOrgUnitId.Text;
string sectionCreateRoute = "/d2l/api/lp/1.0/" + orgUnitId + "/sections/";
var client = new RestClient(host);
var valenceAuthenticator = new D2L.Extensibility.AuthSdk.Restsharp.ValenceAuthenticator(userContext);
var requestCreateSection = new RestRequest(sectionCreateRoute, Method.POST);
valenceAuthenticator.Authenticate(client, requestCreateSection);
而且,我应该提供的 JSON 数据将如下所示。
{
"Name": "Test Section",
"Code": "" ,
"Description": { "Content": "Test", "Type" : "HTML" }
}
如何使用此 JSON 数据创建新部分。
谢谢,
菲利普
我试过这段代码,但它仍然没有创建一个部分。
string orgUnitId = textBoxOrgUnitId.Text;
string sectionCreateRoute = "/d2l/api/lp/1.0/" + orgUnitId + "/sections/";
var client = new RestClient(host);
var valenceAuthenticator = new D2L.Extensibility.AuthSdk.Restsharp.ValenceAuthenticator(userContext);
var requestCreateSection = new RestRequest(sectionCreateRoute, Method.POST);
requestCreateSection.AddJsonBody(new
{
Name = "Section Test",
Code = "156156",
Description = new { Content = "Test", Type = "Html" }
});
valenceAuthenticator.Authenticate(client, requestCreateSection);
因为您使用的是 ResSharp,所以我建议调用以下方法;
public IRestRequest AddJsonBody(object obj)
{
this.RequestFormat = DataFormat.Json;
return this.AddBody(obj, "");
}
但是,为了使用它,您需要一个 C# 对象来表示该数据。 class 定义看起来像这样;
public class NewSectionPostBody
{
public string Name;
public string Code;
public SectionContentType Description;
}
public class SectionContentType
{
public string Content;
public string Type;
}
有了这些和您现有的代码,我可以执行以下操作;
var requestCreateSection = new RestRequest(sectionCreateRoute, Method.POST);
// use object initializer to make instance for body inline
requestCreateSection.AddJsonBody(new NewSectionPostBody{
Name="Test Section",
Code=String.Empty,
Description= new SectionContentType { Content="Test", Type="HTLM" }
});
valenceAuthenticator.Authenticate(client, requestCreateSection);
RestSharp 将对象处理为 json 字符串序列化,因此您基本上可以将任何对象传递给该方法,生成的 json 将用作 post 主体。
最后一件事;如果这是一次性请求,您甚至不需要定义我用于正文的类型。如果您只是删除 classnames ie;
,则可以使用相同的内联初始化构造来创建匿名类型requestCreateSection.AddJsonBody(new {
Name="Test Section",
Code=String.Empty,
Description= new { Content="Test", Type="HTLM" }
});
^^ 我没有实例化用户定义的 class 类型,而是使用匿名类型。如果您不能重复使用构成 post 正文的类型,这是一种更有效的设置请求的方法。