ASP.NET Web API-Startup.cs不存在 [英] ASP.NET Web Api - Startup.cs doesn't exist

查看:366
本文介绍了ASP.NET Web API-Startup.cs不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Web Api解决方案,其中不包含Startup.cs类.我认为这是因为该解决方案并未创建为MVC解决方案.

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.

所有启动代码都在Global.asax.cs文件中定义,如下所示

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);
    }
}

但是,现在我想支持OAuth,并且我发现的所有文档都基于带有以下类的Startup.cs

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?

这与Global.asax.cs类有任何冲突吗?

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

编辑:添加Startup.cs类后,我无法达到添加到其中的断点...

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);
        }
    }
}

知道发生了什么事吗?

推荐答案

Startup.cs是OWIN授权包的一部分.如果未通过NuGet添加该程序包,则无法保证它会起作用.但是,根据此答案判断,它可能仍然可以工作,具体取决于您的环境.

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

简短答案:如果您从NuGet安装了 Microsoft.Owin.Security.OAuth ,那应该很好.否则,您需要安装它.

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

更新: 为了使MVC在启动时调用Configuration方法,还需要从NuGet安装 Microsoft.Owin.Host.SystemWeb 程序包.不需要使用web.config进行更改,IIS会自动检测Owin主机并为您加载它.

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.

这篇关于ASP.NET Web API-Startup.cs不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆