Skip to content

Instantly share code, notes, and snippets.

@davidpeden3
Created February 10, 2017 23:00
Show Gist options
  • Save davidpeden3/1ef73ea305fe262da5aab60150219131 to your computer and use it in GitHub Desktop.
Save davidpeden3/1ef73ea305fe262da5aab60150219131 to your computer and use it in GitHub Desktop.
public override Task ApplyActionAsync(RewriteContext context, BackReferenceCollection ruleBackReferences, BackReferenceCollection conditionBackReferences)
{
var response = context.HttpContext.Response;
response.StatusCode = StatusCode;
if (!string.IsNullOrEmpty(StatusReason))
{
context.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = StatusReason;
}
context.Result = RuleResult.EndResponse;
context.Logger?.CustomResponse(context.HttpContext.Request.GetEncodedUrl());
if (!string.IsNullOrEmpty(StatusDescription))
{
var content = Encoding.UTF8.GetBytes(StatusDescription);
response.ContentLength = content.Length;
response.ContentType = "text/plain; charset=utf-8";
response.Body.Write(content, 0, content.Length);
}
return TaskCache.CompletedTask;
}
@Tratcher
Copy link

return response.Body.WriteAsync...

@Tratcher
Copy link

Tratcher commented Feb 10, 2017

Also, since the StatusDescription is constant, you could Encoding.UTF8.GetBytes(StatusDescription); move to the constructor

@davidpeden3
Copy link
Author

regarding StatusDescription, i'm not following you. it may or may not be set. are you suggesting making additional constructor overloads to account for the combination of possible parameters? i can add:

public CustomResponseAction(int statusCode, string statusReason, string statusDescription)

but can't account for the other two individually since they share the same signature. that's why i initially went with settable properties. if it's valid to say that you can't/won't have a StatusDescription without also having a StatusReason, then that's obviously doable.

what do you suggest?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment