67

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?

Amir Pourmand
  • 519
  • 6
  • 17
user3587624
  • 1,427
  • 5
  • 29
  • 60

5 Answers5

54

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

enter image description here

freedeveloper
  • 3,670
  • 34
  • 39
  • 3
    This is correct answer for VS 2015 - It will generate all the necessary code for you. – Adam W Nov 16 '17 at 17:46
  • 2
    make sure you have Microsoft.Owin.Host.SystemWeb nuget pkg installed. otherwise it will not work – uriz Apr 24 '19 at 07:29
45

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Tyler Stahlhuth
  • 536
  • 8
  • 9
  • 1
    Actually, I added the Startup.cs and the Startup.Auth.cs and it is not breaking in the ConfigurAuth(app) line at all. I added the assemble ([assembly: OwinStartup(typeof(MyGames.Startup))]) above the namespace... Any ideas? – user3587624 Mar 28 '17 at 19:54
  • Did you install the package from NuGet? – Tyler Stahlhuth Mar 28 '17 at 22:31
  • 1
    I did. I have installed this: but also many others like Jwt, Active Directory, etc... – user3587624 Mar 28 '17 at 22:33
  • Do you have under in web.config? – Tyler Stahlhuth Mar 28 '17 at 22:39
  • Actually I did that too (found that on another question) but it didn't work either. What actually surprises me is that I need to do all of that on my solution but on a brand new solution neither the [assembly] tag or the key exist in the solution and the Startup works just fine... – user3587624 Mar 28 '17 at 22:41
  • Do you have any OWIN settings under the tag in web.config? – Tyler Stahlhuth Mar 28 '17 at 22:53
  • I have one for Microsoft.Owin, another for Microsoft.Owin.Security, other for Microsoft.Owin.Security.OAuth etc... but nothing for just Owin – user3587624 Mar 28 '17 at 22:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139311/discussion-between-tyler-stahlhuth-and-user3587624). – Tyler Stahlhuth Mar 28 '17 at 23:00
  • 4
    Just to add here that the issue went gone after adding a reference to Microsoft.Owin.Host.SystemWeb – user3587624 Mar 29 '17 at 17:17
  • Thanks, couldn't figure out why Startup.cs wasn't being called until adding Microsoft.Owin.Host.SystemWeb package – Obakeng Molebatsi Feb 21 '19 at 14:27
8

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))]
SharpLizard
  • 153
  • 1
  • 5
  • 1
    I've had problems with the DLLs not being recompiled. Try deleting the contents from Temporary ASP.Net files (typically in "Appdata/Local/Temp/Temporary ASP.NET Files") and then delete the bin and obj folders for the project to force everything to recompile. – SharpLizard Mar 29 '17 at 16:05
  • 5
    In this case the issue went gone after adding Microsoft.Owin.Host.SystemWeb – user3587624 Mar 29 '17 at 17:18
4

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

<add key="owin:AutomaticAppStartup" value="false" /> 
Antonio Correia
  • 1,093
  • 1
  • 15
  • 22
Peter J
  • 105
  • 1
  • 5
1

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.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
Jawahar
  • 21
  • 1