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

C#asp.net.Netasp.net Mvcasp.net Mvc-4

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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

enter image description here

Solution 2 - C#

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.

Solution 3 - C#

You can add your own startup class, but you need to make sure that Owin is recognizing it. There are several ways to do this , but if you'd like to use a Startup class then you need to use the OwinStartup attribute.

eg:

[assembly: OwinStartup(typeof(MyNamespace.MyStartupClass))]

Solution 4 - C#

My Startup.cs would not run until I removed this line on Web.config (in root folder)

<add key="owin:AutomaticAppStartup" value="false" /> 

Solution 5 - C#

Yes. first you need to remove the following line from your web.config.

<add key="owin:AutomaticAppStartup" value="false" />

Only then it will call the the method it startup.cs.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3587624View Question on Stackoverflow
Solution 1 - C#freedeveloperView Answer on Stackoverflow
Solution 2 - C#Tyler StahlhuthView Answer on Stackoverflow
Solution 3 - C#SharpLizardView Answer on Stackoverflow
Solution 4 - C#Peter JView Answer on Stackoverflow
Solution 5 - C#JawaharView Answer on Stackoverflow