Azure Functions - 如何 return 一个位置 header
Azure Functions - how to return a location header
我是 Azure Functions 的新手,有一个关于如何为新创建的资源生成 Location header 的简单问题。我创建了一个用于创建 Person 的简单函数(原始的,嗯?)。
在我的示例中,我使用 DocumentDB 进行存储。我想 return 一个 Location header 到客户端,然后他们可以 de-reference 如果他们愿意,但要做到这一点,我需要了解路由。
我的代码如下...
public static class PersonProcessing
{
[FunctionName("person")]
public static async Task<HttpResponseMessage> Create(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequestMessage req,
[DocumentDB("Test", "People", CreateIfNotExists = true)]ICollector<Person> outTable,
TraceWriter log)
{
var tx = await req.Content.ReadAsAsync<Person>();
tx.Id = Guid.NewGuid();
outTable.Add(tx);
var response = req.CreateResponse(HttpStatusCode.Created, tx);
response.Headers.Location = new Uri($"{req.RequestUri}/{tx.Id}");
return response;
}
public class Person
{
[JsonProperty("id")]
public Guid Id { get; set; }
public string Name { get; set; }
}
}
我已经根据传入的 RequestUri 创建了 Location header,但是有没有更好(或更标准)的方法来使用 Azure Functions 执行此操作?
我在这里所做的是否是公认的智慧 - 我在网上找不到任何有用的资源因此我的问题?
提前感谢您的回复。
我不知道有什么不同的方法,你的也没有问题。它使用标准的 HttpResponseMessage
模式,而不是发明一种不同的方式来做到这一点。通常,http 触发函数在处理 request.response.
时只使用标准范例
我正在使用 azure functions v2,但上面的方法似乎不起作用。但是我试过了
var headers = req.HttpContext.Response.Headers;
var when = DateTime.UtcNow;
var v = new StringValues(when.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
headers.Add("now", v);
它似乎工作得很好。
另一种方法是让你的函数 return 一个 IActionResult
然后 return 一个 CreatedResult
例如:
[FunctionName("TwoOhWhan")]
public async Task<IActionResult> ReturnATwoOhWhanEvenIfCORSIsInThePicture(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "v1/test/twoohwhan")]
HttpRequest req,
ILogger logger)
{
// ... logic here
var @return = new CreatedResult(location, new
{
id = resourceIdentifier
});
//This trumped me for a while. In ASPNET Core you get this done by using
//the extension method: .WithExposedHeaders("Location")
req.HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "Location");
return @return;
}
我喜欢这种方法,因为它更容易通过 returning 表示 201 本身的结果来收集这里发生的事情。
我偷偷在这个答案之上加了点糖衣。查看关于 return @return
语句的右侧行。如果您正在向 HTTP 触发的函数发出跨源请求,那么您就遇到了麻烦。位置 header 将在 fiddler 或您的网络 chrome 选项卡中可见,但您无法在 JS 代码中访问它。
通常这是在您的 startup.cs
文件中的 aspnetcore 设置中处理的,但 azure 函数无法执行此操作。 Azure 门户仪表板也没有在 Platform Settings
部分下的 CORS
设置页面中为您提供执行此操作的选项。
它不是很直观,互联网上很难找到关于这个小警告的信息。希望这对处于类似情况的其他人有所帮助。
我是 Azure Functions 的新手,有一个关于如何为新创建的资源生成 Location header 的简单问题。我创建了一个用于创建 Person 的简单函数(原始的,嗯?)。
在我的示例中,我使用 DocumentDB 进行存储。我想 return 一个 Location header 到客户端,然后他们可以 de-reference 如果他们愿意,但要做到这一点,我需要了解路由。
我的代码如下...
public static class PersonProcessing
{
[FunctionName("person")]
public static async Task<HttpResponseMessage> Create(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequestMessage req,
[DocumentDB("Test", "People", CreateIfNotExists = true)]ICollector<Person> outTable,
TraceWriter log)
{
var tx = await req.Content.ReadAsAsync<Person>();
tx.Id = Guid.NewGuid();
outTable.Add(tx);
var response = req.CreateResponse(HttpStatusCode.Created, tx);
response.Headers.Location = new Uri($"{req.RequestUri}/{tx.Id}");
return response;
}
public class Person
{
[JsonProperty("id")]
public Guid Id { get; set; }
public string Name { get; set; }
}
}
我已经根据传入的 RequestUri 创建了 Location header,但是有没有更好(或更标准)的方法来使用 Azure Functions 执行此操作?
我在这里所做的是否是公认的智慧 - 我在网上找不到任何有用的资源因此我的问题?
提前感谢您的回复。
我不知道有什么不同的方法,你的也没有问题。它使用标准的 HttpResponseMessage
模式,而不是发明一种不同的方式来做到这一点。通常,http 触发函数在处理 request.response.
我正在使用 azure functions v2,但上面的方法似乎不起作用。但是我试过了
var headers = req.HttpContext.Response.Headers;
var when = DateTime.UtcNow;
var v = new StringValues(when.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
headers.Add("now", v);
它似乎工作得很好。
另一种方法是让你的函数 return 一个 IActionResult
然后 return 一个 CreatedResult
例如:
[FunctionName("TwoOhWhan")]
public async Task<IActionResult> ReturnATwoOhWhanEvenIfCORSIsInThePicture(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "v1/test/twoohwhan")]
HttpRequest req,
ILogger logger)
{
// ... logic here
var @return = new CreatedResult(location, new
{
id = resourceIdentifier
});
//This trumped me for a while. In ASPNET Core you get this done by using
//the extension method: .WithExposedHeaders("Location")
req.HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "Location");
return @return;
}
我喜欢这种方法,因为它更容易通过 returning 表示 201 本身的结果来收集这里发生的事情。
我偷偷在这个答案之上加了点糖衣。查看关于 return @return
语句的右侧行。如果您正在向 HTTP 触发的函数发出跨源请求,那么您就遇到了麻烦。位置 header 将在 fiddler 或您的网络 chrome 选项卡中可见,但您无法在 JS 代码中访问它。
通常这是在您的 startup.cs
文件中的 aspnetcore 设置中处理的,但 azure 函数无法执行此操作。 Azure 门户仪表板也没有在 Platform Settings
部分下的 CORS
设置页面中为您提供执行此操作的选项。
它不是很直观,互联网上很难找到关于这个小警告的信息。希望这对处于类似情况的其他人有所帮助。