I’m trying to extract form data from within the OnActionExecuting method of an ASP.NET Web API Action Filter so I can write it to a log file.
My code looks like this:
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext ctx)
{
if (ctx.Request.RequestUri.Query.StartsWith("admin/")) return;
try
{
_loggingService.LogRequest(new RequestLogModel
{
Handler = ctx.ControllerContext.ControllerDescriptor.ControllerName,
Uri = ctx.Request.RequestUri.AbsoluteUri,
RequestType = ctx.Request.Method.Method,
RequestFrom = ctx.Request.RequestUri.Host,
QueryString = ctx.Request.RequestUri.Query,
FormData = // Need to get form-data here
});
}
catch { }
I can’t seem to work out how to get the form data from the request. Can anyone help?
You should be able to get the form data in the request’s content.
For more information on reading the multipart/form-data content using ReadAsMultipartAsync(…), check out this tutorial, Sending HTML Form Data.