I have an ApiController
which responds to a POST request by redirecting it via HTTP's status code 307. It does so using only information from the header, so the body of the request is not needed by this action. This action is equivalent to:
public HttpResponseMessage Post() {
var url;
// Some logic to construct the URL
var response = new HttpResponseMessage(HttpStatusCode.TemporaryRedirect);
response.Headers.Location = new System.Uri(url);
return response;
}
This is straightforward enough, but there is one improvement I would like to make. The request body could potentially contain a large amount of data, so I would like to leverage the HTTP status code 100 to make this request more efficient. With the controller as it is now, a conversation might look like this:
> POST /api/test HTTP/1.1
> Expect: 100-continue
> ...
< HTTP/1.1 100 Continue
> (request body is sent)
< HTTP/1.1 307 Temporary Redirect
< Location: (the URL)
< ...
Since the request body is not needed by the redirection action, I would like to be able to shorten the conversation to:
> POST /api/controller HTTP/1.1
> Expect: 100-continue
> ...
< HTTP/1.1 307 Temporary Redirect
< Location: (the URL)
< ...
I have spent the better part of a day researching how to accomplish this, and I have not been able to come up with a solution. In my research, I have learned:
- When the
ApiController
's action executes, the100 Continue
has already been sent. - When the
ApiController
is constructed, the100 Continue
has already been sent. - When the
HttpApplication
'sPreRequestHandlerExecute
event is triggered, the100 Continue
response has not been sent. - When a
DelegatingHandler
executes, the100 Continue
has already been sent.
Based on this, the best solution I have come up with so far is to create an HttpModule
which uses the RouteData
on the RequestContext
to override the response when the ApiController
in question is the recipient of the request. This is far from an ideal solution, however, for several reasons (code separation, not taking advantage of Web API's parameter binding, and bypassing additional logic in an AuthorizeAttribute
on the ApiController
).
It seems as if there must be a better solution to this, but I have found very little information on how to properly handle the Expect: 100-continue
header in a Web API application. What would be the simplest way to implement this ApiController
to properly handle the Expect: 100-continue
header?