Using Glimpse With ASP.Net MVC

Introduction

In this article, we will be covering the details of why Glimpse is used and what Glimpse has in store for developers.

                              image

We have been using MiniProfiler in MVC projects to detect the performance of the queries called in the server. The number of requests made by the browser to the server, the response time and many other options a developer would get using these extensions.

Glimpse is a new platform for developers to judge their applications. It is an open-source and free diagnostics platform for the web. This is used to get the information about the ASP.Net Web Forms and ASP.Net MVC applications. In simple words:

Glimpse

Quote:

If you want to understand what exactly is happening on your server, Glimpse will show you.

In this article, we will be using ASP.Net MVC for demonstrating the Glimpse usage. Glimpse is just like Chrome developer tool/Firebug for our server. The dictionary** definition of Glimpse is that it is a momentary or partial view, thus it gives us a glimpse at the inner workings of ASP.Net applications. It does this by gathering detailed diagnostics information about the behaviour and execution of the application and returning the data along with the page with a separate JavaScript overlay. Glimpse shows us what data was sent to the server and what data the server sent back with detailed information regarding exactly what code was executed on the server and how long it took.

Why Glimpse is different

quality


We might now think it is nearly the same as the Chrome developers tools and Firebug tools, but Glimpse is quite different than these tools. Even though, it looks the same as the developer tools but still serves a completely different purpose. Glimpse is more focussed on the server. Let's understand the difference more using the following small diagram:

Chrome developers

Here in the preceding diagram the dev tool takes into consideration the viewpoint of the browser, not the server. All the data we get in the Chrome developer tool is the data that came from the browser, not the server. We can get to understand the time taken for the request from the time the browser sent the request to the time it gets a response back from the server. Here is where Glimpse is useful, it gives the actual time taken by the exact or specific code executed for the request on the server, that is Glimpse byes the Browser in between. See the following diagram:

Glimpse byes the Browser

Glimpse shows us many of information of an MVC application, like what routes are registered, what connection strings are used, what flow was through the MVC pipeline, even the configuration present in the Web.config and what Models are bound.

                                   Smaile

Getting Glimpse

Considering the readers of this article have worked with ASP.Net MVC applications and have used Nuget packages to install plugins and extensions, the easiest way to download and install Glimpse is the Nuget Manager or it can also be installed using the Package manager console. Look at the diagrams below to get both the approaches:

antlr

As you can see in the preceding image, I have already installed Glimpse for MVC5. As we install Glimpse MVC4/5/3, the dependencies like Glimpse Core, Glimpse ASP.Net are also installed and the references are added. The preceding approach is uses the Nuget package. The following diagram is the approach using the Package Manager Console:

Package Manager Console

Both the approaches install the Glimpse with the latest version and also add references for the dependencies. This makes changes to the Web.config adding configuration settings for the Glimpse that is shown below.

Configurations

Custom Section:

The diagram below represents the creation of a section inside the Web.config for Glimpse.

Custom Section

HttpHandler and HttpModules

The diagram below represents the HttpModules and HttpHandlers created for the Glimpse. Glimpse needs a custom HTTP module and HTTP handler with ASP.NET, in the section if you have IIS 6 or earlier, or in the section for later versions of IIS.

There are two types as said above, system.Web and system.webServer.

For system.Web

  1. <system.web>  
  2.     <httpmodules>  
  3.         <add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet">  
  4.     </add></httpmodules>  
  5.     <httphandlers>  
  6.        <add path="glimpse.axd" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" verb="GET">  
  7.     </add></httphandlers>  
  8. </system.web>  
For system.webServer
  1. <system.webserver>  
  2.     <modules>  
  3.         <add name="Glimpse" precondition="integratedMode" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet">  
  4.     </add></modules>  
  5.     <handlers>  
  6.         <add name="Glimpse" path="glimpse.axd" precondition="integratedMode" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" verb="GET">  
  7.     </add></handlers>  
  8. </system.webserver>  
Main Configuration

I said main configuration since all the manipulations to be done lie in changing the configuration for ignoring or adding new tabs to the Glimpse overlay that is done here. The configuration is as in the following::

Main Configuration

Let's look at how the changes are done.

Change Configuration

Configuring the Tabs shown for the Glimpse

There are many tabs shown by default to the user. Here I will show you how to disable any tab, to avoid readability issues. Let's see what tabs are shown. The following is the list.

The default Glimpse tabs are: 
  • Diagnostics

    • Timeline: Glimpse.Core.Tab.Timeline
    • Trace: Glimpse.Core.Tab.Trace

  • ASP.NET

    • Configuration: Glimpse.AspNet.Tab.Configuration
    • Environment: Glimpse.AspNet.Tab.Environment
    • Request: Glimpse.AspNet.Tab.Request
    • Routes: Glimpse.AspNet.Tab.Routes
    • Server: Glimpse.AspNet.Tab.Server
    • Session: Glimpse.AspNet.Tab.Session

  • ASP.NET MVC

    • Execution: Glimpse.Mvc.Tab.Execution
    • Metadata: Glimpse.Mvc.Tab.Metadata
    • Model Binding: Glimpse.Mvc.Tab.ModelBinding
    • Views: Glimpse.Mvc.Tab.Views

  • ADO.NET

    • SQL: Glimpse.ADO.Tab.SQL

These are the default Tabs shown. We will consider only the Diagnostics and the ASP.Net MVC types and how to disable them. The configuration change for disabling is very simple and small. Remember this ignore types are to be added to the Main cofiguration.

  1. <glimpse defaultruntimepolicy="On" endpointbaseuri="~/Glimpse.axd">  
  2.     <tabs>  
  3.         <ignoredtypes>  
  4.             <add type="{Namespace.Type, AssemblyName}">  
  5.         </add></ignoredtypes>  
  6.     </tabs>  
  7. </glimpse>  
Configuring the Runtime policy

Policies control what Glimpse is allowed to do at any given request from the browser. Policy by default is Local Policy that can be disabled and added some other policies.

For example, when we want Glimpse to run remotely, we change the following configuration.
  1. <glimpse defaultruntimepolicy="On" endpointbaseuri="~/Glimpse.axd">  
  2.     <runtimepolicies>  
  3.         <ignoredtypes>  
  4.             <add type="Glimpse.AspNet.Policy.LocalPolicy, Glimpse.AspNet">  
  5.         </add></ignoredtypes>  
  6.     </runtimepolicies>  
  7. </glimpse>  

Start your Glimpse

So far we have discussed how to and from where to add Glimpse to your application and the changes in the Web.config the installation does. As well as the configuration changes we can do. To start the Glimpse we need to run the application and browse through the same host adding:

"host/glimpse.axd"

First let's see a diagram that shows the latest Glimpse UI overay. After getting into the browsed link the page shown is as in the following, that is a part of the page:

Start your Glimpse

After this, we need to click on the Turn Glimpse On. This turns on the Glimpse, then in the application or "/Home/Index" shows a small glimpse "g" icon towards the right bottom. Click on that to see the Glimpse overlay Magic.

Turn Glimpse

The preceding figure shows the jest or a brief view of the request made and time taken, or the Magic Glimpse has. The following figure shows the detailed window for the Glimpse, this has a part of the window but all the default and MVC tabs are enabled and shown to the end user.

MVC tabs

A brief on some important Tabs

Let's discuss briefly on the tabs used by Glimpse or that is displayed to the end user. The tabs let the users understand in detail about their application and even the machine the application is running on. Strange! Let's have a look.

Configuration Tab

This tab gives very useful information about the Machine configurations and Web configurations. It gives the details about the application settings, the connection strings used, Web Authentication modes, the custom errors mode and Default redirect page, the Http handlers and Http Modules. To literally say, this gives the overview of the configuration settings used in Web.config. Have a look at the following image:

Configuration Tab

One can mark in the image or when used in the configuration tab there are connenction strings, that show both the default connection present in the Web.config as well as the default connection strings present in the Machine.config. The XML format, that is /configuration/appSettings, makes it easier to find it in the Web.config.

Environment Tab

This tab gives information about the Server that handles the request. It shows information like the name of the Machine that the .Net Framework application is running. Even the assemblies that are loaded. Let's have a look at the image below showing the Environment tab:

Environment Tab

The preceding image shows the Environment tab and the data it gives. It gives information regarding the Processes running like iisexpress along with its ProcessID, gives information regarding the TimeZone, that would anyhow help developers with the timezone related issues at some point of time. This shows the information about the web Server also like Microsoft-IIS/8.0 running on my machine. Also the application assemblies and the system assemblies are produced in this tab.

Request Tab

This tab would seem to have reduntant data that is also present on the developer tools, but that is not the case. It has the exact data that the server received. This tab solves the problem of the developers trying to trouble-shoot a tricky issue where the request is being modified or intercepted using Fiddler or some other tool before reaching the server or ASP.Net can handle that. Let's have a quick look at the tab image:

Request Tab

As you can see, I have highlighted the query string parameters to show as it is displayed in key/value pairs and also in the actual URL. This gives information about the User Host address and User Agent. The Host address is "::1" since I was running the application on my local system.

Routes Tab

This gives the information about all the routes that are registered and which routes are called for a specific page. This also gives information about the areas if any are used, also the API controllers used and the normal controllers registered in the route.config. Let's have a quick look at the Route tab:

Routes Tab

Here, in the image we see the Constraints and Data tokens highlighted. The greyish background shows the actual route being used for this page. We rarely have constraints and Data tokens defined in our routes, but this information also proves useful. Let's go ahead and look into the code for adding the Constraints and Data token as in the following:

  1. using System.Web;  
  2. using System.Web.Mvc;  
  3. using System.Web.Routing;  
  4.   
  5. namespace KendoMvcDemo  
  6. {  
  7.     public class RouteConfig  
  8.     {  
  9.         public static void RegisterRoutes(RouteCollection routes)  
  10.         {  
  11.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  12.             routes.MapRoute(  
  13.                 name: "Default",  
  14.                 url: "{controller}/{action}/{id}",  
  15.                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },  
  16.                 constraints: new RouteValueDictionary { { "id", @"\d" } }  
  17.                ).DataTokens = new RouteValueDictionary { { "data""value" } };  
  18.         }  
  19.     }  
  20. }  
This is the Code for adding the constraints, here in the constraints we have added a regular expression to mandate the field id (parameter) to be a single digit value only. We have given some random values to the data tokens.

We can see these data in the Route tab image provided. Now, when we run the application after adding these constraints, we would get an IIS forbidden error since the default route now expects an id that must be a single digit.

Session Tab

You might observe in the preceding images everywhere, the Session is disabled/blackened. This is because we do not have any session defined for this page. Once we define the session for a specific page, then we can view the session to be active with the key/value pairs. See the image below that shows the Session defined and the value.

Session Tab

Here, I have defined a session like:
  1. public ActionResult Index()  
  2. {  
  3.     Session["Test"] = 1;  
  4.     var blackshilling = new BlackshillingEntities();  
  5.     ViewBag.Properties = blackshilling.Properties;  
  6.     ViewBag.Message = "Welcome to ASP.NET MVC!";  
  7.   
  8.     return View();  
  9. }  
You can see the value of Session Test as 1 in the image.

Points of Interest

We have discussed much on the Glimpse use and the important tabs. I hope this package is handy for developers to enhance and understand their application better. This would help out during the optimization phase, where it would be required to judge the SQL operations their execution time and many. I will be writing a tip or article on this in the future.

Up Next
    Ebook Download
    View all
    Learn
    View all