70

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?

5
  • 3
    make sure you have added [assembly: OwinStartup(typeof(AuthorizationServer.Startup))] this above the startup class Commented Mar 28, 2017 at 17:50
  • 2
    I added it but still doesn't hit the breakpoint when I run the application. Commented Mar 28, 2017 at 22:02
  • @user3587624 i have the same problem - did you solve it? Commented Nov 21, 2017 at 9:27
  • 3
    My issue went gone when I added a reference to Microsoft.Owin.Host.SystemWeb in my solution. Commented Nov 21, 2017 at 19:51
  • Does this answer your question? OWIN Startup Class Missing Commented Dec 3, 2019 at 7:15

4 Answers 4

53

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

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

This is correct answer for VS 2015 - It will generate all the necessary code for you.
make sure you have Microsoft.Owin.Host.SystemWeb nuget pkg installed. otherwise it will not work
47

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.

10 Comments

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?
Did you install the package from NuGet?
I did. I have installed this: <package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net46" /> but also many others like Jwt, Active Directory, etc...
Do you have <add key="owin:AutomaticAppStartup" value="true" /> under <appSettings> in web.config?
Just to add here that the issue went gone after adding a reference to Microsoft.Owin.Host.SystemWeb
|
5

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

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

2 Comments

Thanks for this. Spent all day trying to work this out and this was the answer.
This was it for me as well.
2

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.

2 Comments

This looks like a bit of a non-answer on an old question.
@Jason the actual code was hidden because of missing formatting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.