Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Web Api - Startup.cs doesn't exist

I have an ASP.NET Web Api solution which doesn't contain a Startup.cs class. I presume this is because the solution wasn't created as an MVC solution.

All the code for the startup is defined in the Global.asax.cs file as you can see below

public class Global : HttpApplication {     void Application_Start(object sender, EventArgs e)     {         // Code that runs on application startup         AreaRegistration.RegisterAllAreas();         GlobalConfiguration.Configure(WebApiConfig.Register);         RouteConfig.RegisterRoutes(RouteTable.Routes);     } } 

However, now I want to have support for OAuth and all the documentation that I've found is based on a Startup.cs with the following class

public partial class Startup {     public void Configuration(IAppBuilder app)     {         ConfigureAuth(app);     } } 

Is it possible to just add this new class in my solution and the solution will continue working?

Will this have any conflicts with the Global.asax.cs class?

EDIT: After I added the Startup.cs class, I can't hit the break point I added into it...

using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.Owin; using Owin;  [assembly: OwinStartup(typeof(MyGame.Startup))]  namespace MyGame {     public partial class Startup     {         public void Configuration(IAppBuilder app)         {             ConfigureAuth(app);         }     } } 

Any idea what's going on?

like image 804
user3587624 Avatar asked Mar 28 '17 17:03

user3587624


People also ask

How do I get Cs go startup?

As we know Startup. cs contains 2 methods, ConfigureServices() and Configure() and we register the dependency and services in ConfigureServices() and Middlewares in Configure() method. Now in asp.net 6.0, Program. cs , is the place where you need to register your services and dependencies after the builder.

What is the use of startup CS in Web API?

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 Cs in ASP.NET Core?

The Startup classASP.NET Core apps use a Startup class, which is named Startup by convention. The Startup class: Optionally includes a ConfigureServices method to configure the app's services. A service is a reusable component that provides app functionality.


2 Answers

If you have installed the Owin packages, you can simple create the start up class with:

enter image description here

like image 181
freedeveloper Avatar answered Sep 21 '22 18:09

freedeveloper


Startup.cs is a part of the OWIN authorization package. If the package isn't added through NuGet, I can't guarantee it would work. However, judging by this answer, it might work anyway depending on your environment.

https://stackoverflow.com/a/24678109/6442626

Short answer: If you installed Microsoft.Owin.Security.OAuth from NuGet, that should be good. Otherwise, you need to install it.

Update: In order to get MVC to call the Configuration method in startup, you also need to install the Microsoft.Owin.Host.SystemWeb package from NuGet. There is nothing special you need to change with web.config, IIS will automagically detect the Owin host and load it for you.

like image 36
Tyler Stahlhuth Avatar answered Sep 23 '22 18:09

Tyler Stahlhuth