Category: NuGet

MVC – get some help

If you’re using MVC and you’re looking for some in-depth information on basically whats going on when you run the code then you should take a look at Miniprofiler (from the guys at StackOverflow) or Glimpse.


Miniprofiler can be added in to your solution and can give you all sorts of really useful information such as which view is currently being rendered and from which method in the controller (which is not always obvious). this can be quite a time saver if you’re not used to the codebase and just want to quickly find the ActionResult which has been hit.

To Add MiniProfiler to an MVC application install it using the Nuget Package Manager console. (you need to use the console sometimes if the package uses a Powershell script)

Once added via Nuget browse to your global.asax file and add in the following 2 methods:-

[sourcecode language=”csharp”] protected void Application_BeginRequest()
{
if (Request.IsLocal)
{
MiniProfiler.Start();
}
}
[/sourcecode]

and then :-

[sourcecode language=”csharp”] protected void Application_EndRequest()
{
MiniProfiler.Stop();
}
[/sourcecode]

Rebuild your code and run your application and you’ll see info being displayed by MiniProfiler in the top left hand corner as follows:-

Glimpse


Glimpse is a JavaScript plug-in which informs you of information such as Route Debugging which is a superb feature. Add Glimpse via Nuget and then browse to your site and add /glimpse.axd to the end of the url from your application:-

Glimpse

Pres enter and you’ll be taken to this screen, this nice bit here is you can drag the ‘Turn Glimpse On’ and ‘Turn Glimpse Off’ to your fav bar and just click them to do as it says on your site. (This is all done in JavaScript and has very little overhead)

Glimpse

After clicking on ‘Turn Glimpse On’ we see the little eyeball bottom right of the screen:-

Glimpse

When clicking on that you’ll see something like the following:-

Glimpse
Here we can drill into all sorts of information such as:-

  • What route has been called
  • Which view has been rendered
  • Time spent to render the page and timings for each part
  • Config on the server

Again go get them and you’ll have them up and running very quickly and benefit from the features they come with.



AttributeRouting for MVC

AttributeRouting is a fantastic open source Nuget package which allows you to specify routes using attributes on your MVC controllers and actions. We use it in all of our MVC projects at work and I highly recommended it, saves you having to map all y.

Attribute Routing

The QuickStart Guide can be found here.

Main reasons to use AttributeRouting

•Decorate your actions with GET, POST, PUT, DELETE, and verb agnostic attributes.
•Map multiple routes to a single action, ordering them with an Order property.
•Specify route defaults and constraints inline and also using attributes.
•Specify optional params inline with a token before or after the parameter name.
•Specify the route name for supporting named routes.
•Define MVC areas on a controller or base controller.
•Group or nest your routes together using route prefixes applied to a controller or base controller.
•Support legacy urls.
•Set the precedence of routes among the routes defined for an action, within a controller, and among controllers and base controllers.
•Generate lowercase outbound urls automatically.
•Define your own custom route conventions and apply them on a controller to generate the routes for actions within the controller without boilerplate attributes (think RESTful style).
•Debug your routes using a supplied HttpHandler.

Go check it out you wont be disappointed.



MVC Glimpse via NuGet

Glimpse is basically FireBug for MVC applications and it is written entirely using JavaScript – if you havent checked it out and you’re using MVC at all then please check this out asap.

It takes 30 seconds to install and have running on your application, it shows you details about everything that’s happening on the server, less talking go get it now!

Click —-> to Download Glimpse now.

Enjoy!



Use Nuget for Jquery and jQueryUI Updates and more

If you’re using Visual Studio 2010 and your web application is using jQuery and or jQueryUI, you want to keep up to date and make sure everyone has the most up to date versions.

Use NuGet to add the latest and greatest versions of these libraries into your application and no longer will you have to go find the latest version, download, unzip and then add to your web app.

NuGet has a very similar way of adding and removing libraries such as you would use add/remove programs within Windows.

Scott Hanselman has an upcoming talk at TechEd on NuGet for the Enterprise which will cover how to use NuGet and setup a NuGet Server within your work – looking forward to that.

That’s it enjoy.



ELMAH, using Nuget what they are and why you should use them

ELMAH stands for Error Logging Modules and Handlers and if you have an ASP.NET web application or web site and you want a very easy way to use it then this article is for you.

Note I am going to use NuGet to add ELMAH to an existing application using Visual Studio 2010.

For this tutorial I am going to sue the NuGet Package Console instead of using the other option which is ‘Add Library Package Reference’, let’s get started:-

1 – Open an existing VS2010 web application and then select ‘Tools’, ‘Library Package Manager’ and then select ‘Package Manage Console’.
2 – One you have the console window check to make sure your Package Source is pointing to the NuGet Official package source and the type:-

List-Package -remote

and then press enter

3 – This will list all available packages which you can add to your application – Note if you have several projects within your solution make sure you have the project you want to add ELMAH to selected in the console right hand drop down, below I am looking at the official packages and adding to my MVC site as you can see:-

Package Manager Console

4 – Ok now type:-

Install-package ELMAH

and then press enter

check now and you will see that ELMAH has indeed been added and your web.config has changed with all the settings for ELMAH having been added to your file – Uninstall-package ELMAH would undo this work.

5 – Thats it, ELMAH has been added to your project. To test it working and as an example add a testpage and just throw an exception on it, or browse to a page that doesnt exist within your app and then in your web browser url browse to /elmah.axd – Note this is logging into the app code folder and writing to .xml files – part 2 of this blog post will discuss logging using SQL instead of using this method.

6 – Voila ELMAH is logging all your app errors.



Creating and then using a NuGet local repository

In this blog post I am going to cover the creation and usage of a local NuGet repository.
In this example I am going to add the Microsoft Bits (Background Intelligence Transfer Service) dll to a web application.

1 – First of all I created a folder on my hard drive simply called NuGetPackages. (c:\NuGetPackages)

2 – Fire up VS 2010 and then create a new web application or open an existing one so that we can use this to test the new package were creating.

3 – Once you have the web application open, you’ll want to change where Visual Studio 2010 looks for the packages locally, to do this go to the Tools menu, and select Library Package Manager, from there then select Package Manager Settings (as seen below)

Screen shot of the menu to change the package settings

Once selected we need to change where Visual Studio and more specifically where NuGet is looking to try to find our local packages. Type a name and then paste in the location in the source textbox and press Add. You should see something like this:-

Change the location of where to find your packages

4 – Ok at this point we have only created a location to put the packages, and told NuGet where to locate them. Now we need to create a package.

5 – Time to create a package, first of all you need to create a file with the extension .nuspec and this file will be an xml file which will have the following structure:-

Element
Description
id The identifier for the package. It used as a partial name of the package.
version The package version. Also used as a partial name of the package.
authors Author(s) name. Use a comma to separate multiple authors.
description Describe your package for others to understand what the package is used for.

I created a file called Bits.nuspec and put this into the folder created in step 1) (c:\NuGetPackages) you can see my file below:-

Example content for a .nuspec file

6 – Now we need to build the package and to do this we need to have downloaded the Nuget command line tool which you can get from here:- Nuget command line tool

7 – To make life easier I copied this file (Nuget.exe) to the folder in step 1 (c:\NuGetPackages), now open a command prompt and then change to this directory c:\NuGetPackages, now to create the package just type:-

Nuget.exe pack Bits.nuspec

This should create a file with the extension .nupkg for me this was Bits.2.0.0.0.nupkg as it uses the version information as part of the name. (The version info comes from the .nuspec file)

8 – So far we have created a package and if you look at the last part of the code in Step 5, you’ll see a reference to files, this has *.dll and in this instance will add any dll’s found in this folder to the package. This would not be what you want if your going to be adding lots of different dll’s for other packages, you can change this line to read:-

<file src=”\bin\Release\*.dll” target=”lib” />

Where would be changed to relate to the package you want to add – the line above is just used as an example.

9 – To Install the package we will use the Package Manager Console from within VS 2010 which is installed when you install NuGet firstly you can type List-Package to see all the packages you have and then type Install-Package Bits to install the package called Bits.

10 – Now we should be ready to try it out – go back to VS 2010 and then this time select Tools, Library Package manager and then Add Library Package Reference as below:-

Add a library package

Select the package we installed, in this case Bits and click Install as below:-

Install a package

That’s all there is to it, hope someone out there found this of use.