Skip to content

Instantly share code, notes, and snippets.

@im-kashi
Created March 6, 2019 08:54
Show Gist options
  • Save im-kashi/2e5b1e23b49f5ee782df95f67ba07dc0 to your computer and use it in GitHub Desktop.
Save im-kashi/2e5b1e23b49f5ee782df95f67ba07dc0 to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace Inasync {
/// <summary>
/// AdX OpenRTB に適したレスポンスにする属性。
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class AdxOpenRtbResponseAttribute : Attribute, IAsyncResultFilter {
/// <summary>
/// 強制的に No Bid にするか否か。
/// </summary>
public bool ForceNoBid { get; set; } = false;
public Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) {
// Http Response Header
// https://developers.google.com/authorized-buyers/rtb/openrtb-guide#checking-the-openrtb-version
context.HttpContext.Response.Headers.Add("x-openrtb-version", "2.5");
if (ForceNoBid || context.Result is ObjectResult result && result.Value == null) {
// No Bid Response
// https://developers.google.com/authorized-buyers/rtb/openrtb-guide#no-bid
context.Result = new StatusCodeResult(204);
}
return next();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment