Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the new Startup.cs file for in Visual Studio 2013 projects?

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?

like image 797
Mark Redman Avatar asked Nov 17 '13 17:11

Mark Redman


People also ask

What does startup CS file do?

When doing so, the app will include a Startup. cs file that is responsible for setting up request middleware in a way that's very similar to how ASP.NET Core behaves. If you need to run code when your ASP.NET MVC app starts up, it will typically use one of these approaches.

What is Startup Auth Cs in MVC?

Yes, Startup.Auth.cs comes to support OWIN authentication. While creating the application, by default Individual User Account will be selected and hence you get those files. if you want no authentication then while creating new project under Configure Authentication button select No Authentication.

What is Startup CS in Web API?

Startup. cs file contains Startup class which triggers at first when application launches and even in each HTTP request/response. Actually, the inception of Startup class is in OWIN application which is a specification to reduce dependency of application on server.


3 Answers

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

like image 88
Lin Avatar answered Oct 20 '22 21:10

Lin


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.

like image 25
user247702 Avatar answered Oct 20 '22 21:10

user247702


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

like image 45
cecilphillip Avatar answered Oct 20 '22 21:10

cecilphillip