Did it again today: For logging purposes I needed to capture the full Request.Form data as a string and while it’s pretty easy to retrieve the buffer it always takes me a few minutes to remember how to do it. So I finally wrote a small helper function to accomplish this since this comes up rather frequently especially in debugging scenarios or in the immediate window.
Here’s the quick function to get the form buffer as string:
/// Returns the content of the POST buffer as string
public static string FormBufferToString()
HttpRequest Request = HttpContext.Current.Request;
if (Request.TotalBytes > 0)
Clearly a simple task, but handy to have in your library for reuse. You probably don’t want to call this if you have a massive inbound form buffer, or if the data you’re retrieving is binary. It’s probably a good idea to check the inbound content type before calling this function with something like this:
var formBuffer = string.Empty;
if (Request.ContentType.StartsWith("text/") ||
Request.ContentType == "application/x-www-form-urlencoded") )
formBuffer = FormBufferToString();
to ensure you’re working only on content types you can actually view as text.
Now if I can only remember the name of this function in my library – it’s part of the static WebUtils class in the West Wind Web Toolkit if you want to check out a number of other useful Web helper functions.
I'll be at DevIntersection in Vegas this fall giving sessions on ASP.NET Core with Angular and Localization. Thinking of coming? Use discount code STRAHL and save a few bucks. If you do be sure to stop by and say hello!
Other Posts you might also like