Skip to main content
deleted 18 characters in body
Source Link
Tom
  • 7.7k
  • 1
  • 26
  • 49

I try to log all posted data in my WebApi when a exception is thrown. I have try this:

        protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        if (request.Content == null) 
        return await base.SendAsync(request, cancellationToken);
        
        var Body = await request.Content.ReadAsStringAsync();
     
    request.Properties["body"] = Body;

        return await base.SendAsync(request, cancellationToken);
    }

And in my error log method i get the posted values in Properties["body"]Properties["body"].

But when the users upload a image iI get this when iI try to get the files: This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked.

at System.Web.HttpRequest.get_Files()

This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked.
at System.Web.HttpRequest.get_Files()

How can iI store only posted data(text)data(text) in this property?

I try to log all posted data in my WebApi when a exception is thrown. I have try this:

        protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        if (request.Content == null) return await base.SendAsync(request, cancellationToken);
        
        var Body = await request.Content.ReadAsStringAsync();
        request.Properties["body"] = Body;

        return await base.SendAsync(request, cancellationToken);
    }

And in my error log method i get the posted values in Properties["body"].

But when the users upload a image i get this when i try to get the files: This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked.

at System.Web.HttpRequest.get_Files()

How can i store only posted data(text) in this property?

I try to log all posted data in my WebApi when a exception is thrown. I have try this:

protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
    if (request.Content == null) 
        return await base.SendAsync(request, cancellationToken);

    var Body = await request.Content.ReadAsStringAsync();
 
    request.Properties["body"] = Body;

    return await base.SendAsync(request, cancellationToken);
}

And in my error log method i get the posted values in Properties["body"].

But when the users upload a image I get this when I try to get the files:

This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked.
at System.Web.HttpRequest.get_Files()

How can I store only posted data(text) in this property?

Source Link
mrcode
  • 567
  • 2
  • 6
  • 19

Log request in ASP.NET Web Api

I try to log all posted data in my WebApi when a exception is thrown. I have try this:

        protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        if (request.Content == null) return await base.SendAsync(request, cancellationToken);
        
        var Body = await request.Content.ReadAsStringAsync();
        request.Properties["body"] = Body;

        return await base.SendAsync(request, cancellationToken);
    }

And in my error log method i get the posted values in Properties["body"].

But when the users upload a image i get this when i try to get the files: This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked.

at System.Web.HttpRequest.get_Files()

How can i store only posted data(text) in this property?