2

I have a ASP.NET WebAPI application and some of the request exceed the maximum lenght. That's fine, I know I can increase the limit either in IIS or web.config, but that's not my point.

When I read the contents of the request using one of the Request.Content.Read methods all of them return a Maximum request length exceeded exception.

I would like to retrieve the first characters of the body though, for logging purposes.

Is it still possible to read just the first characters of the body? Or is the whole content impossible to retrieve?

Community
  • 1
  • 1
vgaspar.travix
  • 326
  • 1
  • 9
  • 5
    No' it's not possile, when IIS receives the content size will stop reading if it's larger than the max length allowed. – Gusman Jan 27 '17 at 10:58
  • Are you uploading a file? Could you implement an HttpModule to Handle File Uploads? Here's more info on that: https://weblogs.asp.net/jongalloway/large-file-uploads-in-asp-net – felipekm Jan 27 '17 at 11:39
  • @felipekm the expected format of the application is json, but the idea is that I would like to check the first characters/bytes of any request that I get , so I can understand what we received. It's a company internal API and that would help debugging communication issues between APIs. – vgaspar.travix Jan 27 '17 at 11:47
  • Yeah I see, have you tried to intercept it on global.asax? – felipekm Jan 27 '17 at 11:54
  • @felipekm no i haven't, although i understood from some entries on google that that might help. The reason I didn't try is that intercepting it in the ``Application_BeginRequest`` is too early for us in the lifetime of the application, because for example we don't have access to a logging service required to log the problem. But if it is possible to solve this problem there we will definitely try. – vgaspar.travix Jan 27 '17 at 11:59
  • Yeah I got it, on my view you have two options, 1 - find another way to log it or instantiate your log object on the Application_BeginRequest or 2 - you can develop a native module for it, more information about a creating a native module can be found here (using C++): – felipekm Jan 27 '17 at 12:08

1 Answers1

1

you must catch the exception on global.asax file, then you will have the request data.

see these 2 links:

Catching "Maximum request length exceeded" http://geekswithblogs.net/sglima/archive/2011/09/20/how-to-handle-maximum-request-length-exceeded-exception.aspx

Community
  • 1
  • 1
Fábio
  • 11
  • 1
  • You should not provide an answer with only a link to an external resource because if the resource is no longer available, this answer will become useless. You should try to add a minimal example in the answer. See [Your answer is in an other castle](http://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is-an-answer-not-an-answer) – ᴄʀᴏᴢᴇᴛ Jan 27 '17 at 13:42