Question

I have just installed Visual Studio 2013, created an MVC Web Application project and noticed a new file in the project template called Startup.cs.

What is this, how is this different from Global.asax.cs and are there any good best practices on what to use this for?

Was it helpful?

Solution

Every OWIN application has a startup class where you specify components for the application pipeline.

If you start a new Visual Studio project, you'll see pieces of OWIN in it. OWIN is a specification that defines an API for framework and servers to cooperation. The point of OWIN is to decouple server and application. For example, ASP.NET Identity uses OWIN security, SignalR self hosting uses OWIN hosting, and etc., the examples all use OWIN, therefore they all need to have a startup class, that is defined in "Startup.cs" file.

The Global.asax, the ASP.NET application file, is an optional file that contains code for responding to application-level events raised by ASP.NET or by HttpModules.

For more details:

OWIN

http://www.asp.net/aspnet/overview/owin-and-katana

Global.asax

http://msdn.microsoft.com/en-us/library/1xaas8a2(v=vs.71).aspx

You can find more ideas about why OWIN in the following article:

http://www.asp.net/aspnet/overview/owin-and-katana/an-overview-of-project-katana

OTHER TIPS

The file seems to be related to SignalR. Quoting the VS 2013 release notes:

Built on OWIN

SignalR 2.0 is built completely on OWIN (the Open Web Interface for .NET). This change makes the setup process for SignalR much more consistent between web-hosted and self-hosted SignalR applications, but has also required a number of API changes.

MapHubs and MapConnection are now MapSignalR

For compatibility with OWIN standards, these methods have been renamed to MapSignalR. MapSignalR called without parameters will map all hubs (as MapHubs does in version 1.x); to map individual PersistentConnection objects, specify the connection type as the type parameter, and the URL extension for the connection as the first argument.

The MapSignalR method is called in an Owin startup class. Visual Studio 2013 contains a new template for an Owin startup class; to use this template, do the following:

  1. Right-click on the project
  2. Select Add, New Item...
  3. Select Owin Startup class. Name the new class Startup.cs.

In a Web application, the Owin startup class containing the MapSignalR method is then added to Owin's startup process using an entry in the application settings node of the Web.Config file, as shown below.

In a Self-hosted application, the Startup class is passed as the type parameter of the WebApp.Start method.

The Startup class is the convention that Katana/OWIN looks for to initialize the pipeline. When your app starts, the code inside of the Configuration function is run to set up the components that'll be used. In the MVC 5 templates, it's used to wire up the authentication middleware which is all built on top of OWIN.

If you want to use dependency injection with OWIN, check out this project on GitHub: DotNetDoodle.Owin.Dependencies

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