How do I use MiniProfiler with a single page web app / REST backend?
Asked Answered
B

1

20

I have a single page javascript application (done with JavascriptMvc) and a backend with REST services built on top of ASP.NET MVC3 (done with NServiceMVC).

The REST services have MiniProfiler installed and running, and the X-MiniProfiler-Ids headers come back with each AJAX request. I do actually have miniprofiler running and working, but I could not find any info on this, and so I am not sure if I'm doing it the right way.

Is this a supported scenario, and is there a specific way to do this now?


What I am currently doing is this:

In the HTML app (which is all static code, no dynamic stuff), I have:

<script type="text/javascript" src="/api/profiler"></script>

In my MVC app, I have:

    public ActionResult Profiler()
    {
        if (!ControllerContext.HttpContext.IsDebuggingEnabled)
        {
            return new EmptyResult();
        }
        return new ContentResult() { 
            Content = StackExchange.Profiling.MiniProfiler.RenderIncludes(
                        position: RenderPosition.Right,
                        showControls: true
                      )
                      .ToString()
                      .Replace("<script type=\"text/javascript\">", "")
                      .Replace("</script>", "") 
        };
    }

Clearly, there is a hack in here to strip out the hardcoded <script> tags.

Other than this, from the rest of the MVC side of things, profiler is used exactly the same as usual. When you do an action in the app that causes a REST call to happen, miniprofiler shows it up in the corner. Using the showControls:true parameter is pretty helpful here too so the clear button shows up, because otherwise you just get a constant list of actions since the entire page essentially never refreshes.

Is this the "correct" way to do this, or is there a better way?

Boa answered 22/3, 2012 at 21:49 Comment(4)
Just to be clear: your solution works, but you want confirmation that you've been following best practices?Telegraphy
@KirkWoll Yes. Because there is not very much documentation on profiler, I want to know if I'm overlooking an existing solution. Worst case this question can provide some documentation for future web searches. If this is the "best" way I will probably submit a patch so I don't have to do my .replace() hack and make this the real "official" way.Boa
I have the same question. Are you still doing it this way?Radioactive
should this be on codereview.stackexchange.com?Delinda
H
1

There is an assumption in your code that @MiniProfiler.RenderIncludes() generates all of it's content using javascript. While that may be a valid assumption at the current time, this could change.

Why not simply avoid the hacks altogether and use an ajax request to load the profiler? In either scenario, whether ajax or embedded script, the act of inserting the profiler via a callback will affect the outcome of profiling somewhat.

<div id="profiler"></div>
<script type="text/javascript">
  $.load("#profiler","api/profiler");
</script>
Hyrup answered 4/3, 2014 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.