【发布时间】:2020-04-04 10:23:45
【问题描述】:
我的代码如下:
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
object[] eventGridMessages = (object[])actionContext.ActionArguments.FirstOrDefault().Value;
if (eventGridMessages == null || (eventGridMessages != null && eventGridMessages.Length <= 0))
{
throw new ArgumentNullException("eventGridMessages", "eventGridMessages parameter cannot be empty.");
}
if (actionContext.HttpContext.Request.Headers.ContainsKey("aeg-event-type") && actionContext.HttpContext.Request?.Headers["aeg-event-type"].ToString() == "SubscriptionValidation")
{
actionContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
var x = this.GetValidationCodeResponse(eventGridMessages[0].ToString());
actionContext.HttpContext.Response.ContentType = "application/json";
actionContext.HttpContext.Response.Body = new MemoryStream(Encoding.UTF8.GetBytes(x, 0 , x.Length));
}
return;
}
/// <summary>
/// Gets the validation code response.
/// </summary>
/// <param name="validationRequest">The validation request.</param>
/// <returns>Validation Response Message</returns>
private string GetValidationCodeResponse(string validationRequest)
{
var validationRequestJson = JsonConvert.DeserializeObject<CustomEvent<EventSubscritionValidationCode>>(Convert.ToString(validationRequest));
var validationResponse = JsonConvert.SerializeObject(new { validationResponse = "644e4387-465f-46ee-8df9-b5330c3bda69" });
return validationResponse;
}
注意:
我正在执行AzureEventGrid 操作。出于身份验证的目的,我正在实现这个包含握手代码的过滤器。以前在框架中,我们有一个HttpResponseMessage类型的响应,我们可以将数据设置为
actionContext.Response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("JSON data")
}
但在 .NET Core 中,它已更改为 HttpResponse,它是一个抽象类,我无法在其中创建实例,也无法将 JSON 响应设置为 HttpResponse。
我无法在 HttpResponse 上看到我的 JSON 数据 (GUID)。
有人可以帮我解决这个问题吗?
【问题讨论】:
标签: c# asp.net .net azure azure-eventgrid