Question

I have followed all steps given in the MiniProfiler document and able to see the result for a specific call.

But when trying to add it to the Global.asax page, it is throwing an error like:

Exception Details: System.Web.HttpException: Request is not available in this context

Does MiniProfiler work in ASP.NET website also or does it work only with a web application?

I am adding the package through the NuGet MiniProfiler.

Code of Global.asax page

<%@ Application Language="C#" %>
<%@ Import Namespace="StackExchange.Profiling" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        MiniProfiler.Start();
    }

    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
        MiniProfiler.Stop();
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

</script>
Was it helpful?

Solution

I was slightly wrong, last time I used this was with ASP.MVC. But I've managed to get a working example for you to try:

Global.ASAX

private bool isStarted;

protected void Application_Start(object sender, EventArgs e)
{
    this.isStarted = false;
}

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (!isStarted)
    {
         this.isStarted = true;

         if (Request.IsLocal) 
         {
              MiniProfiler.Start();   
         }
    }
}

protected void Application_EndRequest(object sender, EventArgs e)
{
     MiniProfiler.Stop();
}

Page.aspx

protected void Page_Load(object sender, EventArgs e)
{
  var miniprofiler = MiniProfiler.Current;

  var htmlString = miniprofiler.Render();

  Literal1.Text = htmlString.ToString();
}

It's going to need some tweaking but at least it'll give you something to work with. I'm not entirely convinced that mini-profiler is designed to work with ASP.Net web forms though.

Oh and Literal1 is just a standard Literal control.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top