2

in the global.asax to measure the request execution time in the onbeginrequest (start the stopwatch) and onendrequest (calculate the difference).

then in the end request do response.write with the result.

however it writes the result AFTER the closing html tag. basically appends to the end.

current line of code is:

HttpContext.Current.Response.Write(elapsedTime);

is there an easy way for the response write to REPLACE the string ::actualResult:: within the actual html with the actual result string from the response write?

i've tried a lot of things including searching online but seems no one needs this or i suck at searching. i thought i could just get the entire response somehow and replace from there but unsure how to do that... something along ...Response.GetTheEnitreResponse??.Replace... of course that is just wishful thinking ;)

thnx

b0x0rz
  • 3,953
  • 8
  • 52
  • 82

2 Answers2

0

You didn't specify if you were using web forms, MVC, web pages, etc. but normally these frameworks have buffered a response that has been output by whatever page the user is hitting. Your code in onendrequest is coming to the party after all of the page contents (normally closed with an html closing tag) has been output to the buffer. So when you do a Response.Write you are appending to that html, thus it is outside the closing html tag.

If you want to have the timing be visual on the page you will have to parse into the response and inject your string. This looks hard to do outside of a Page class in ASP.NET.

Messy, and there are better alternatives. Tracing is usually the way these types of things are handled.

You may want to consider writing this information out to a Glimpse trace or somehow hooking into its display... I can't say enough about Glimpse.

JasonCoder
  • 1,126
  • 2
  • 12
  • 24
  • If you want to have the timing be visual on the page you will have to parse into the response and inject your string. This looks hard to do outside of a Page class in ASP.NET. << exactly lol therefore my question ;) – b0x0rz Sep 05 '14 at 14:46
  • 1
    I feel you, I was just trying to offer alternatives since it's not easy to do directly what you want. Not for the faint of heart but I found an article by stud blogger Rick Strahl that might give you some ideas: http://weblog.west-wind.com/posts/2009/Nov/13/Capturing-and-Transforming-ASPNET-Output-with-ResponseFilter – JasonCoder Sep 05 '14 at 14:55
0

Rather than writing the elapsed value to the response, you could store the result in HttpContext.Items and then access this on the view/page:

HttpContext.Current.Items.Add("elapsed", elapsed);

HttpContext.Items Property

Phil Cooper
  • 3,083
  • 39
  • 63
  • sorry, but no. i tried that. the problem is the page is already done when the request end triggers and so it doesn't work. – b0x0rz Sep 05 '14 at 14:45
  • 1
    @b0x0rz Ok, you might want to have a look at http://miniprofiler.com/, it'll give you more power over what you profile of the lifetime of the request and enable you to have breakdown of what time is being spent and where. – Phil Cooper Sep 08 '14 at 10:19