use*_*624 38 .net c# asp.net asp.net-mvc asp.net-mvc-4
我有一个ASP.NET Web Api解决方案,它不包含Startup.cs类.我认为这是因为解决方案不是作为MVC解决方案创建的.
启动的所有代码都在Global.asax.cs文件中定义,如下所示
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);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,现在我想支持OAuth,我发现的所有文档都基于具有以下类的Startup.cs
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
Run Code Online (Sandbox Code Playgroud)
是否可以在我的解决方案中添加这个新类,解决方案将继续工作?
这会与Global.asax.cs类有任何冲突吗?
编辑:我添加Startup.cs类后,我无法点击我添加到它的断点...
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
知道发生了什么事吗?
fre*_*per 31
如果您已经安装了Owin软件包,则可以使用以下命令简单地创建启动类:
Tyl*_*uth 27
Startup.cs是OWIN授权包的一部分.如果没有通过NuGet添加包,我不能保证它会起作用.但是,从这个答案判断,它可能会取决于您的环境.
简短回答:如果您从NuGet安装了Microsoft.Owin.Security.OAuth,那应该是好的.否则,您需要安装它.
更新:为了让MVC在启动时调用Configuration方法,您还需要从NuGet安装Microsoft.Owin.Host.SystemWeb包.您无需使用web.config更改任何特殊内容,IIS将自动检测Owin主机并为您加载它.
小智 5
您可以添加自己的启动类,但需要确保Owin正在识别它.有几种方法可以执行此操作,但如果您要使用Startup类,则需要使用OwinStartup属性.
例如:
[assembly: OwinStartup(typeof(MyNamespace.MyStartupClass))]
Run Code Online (Sandbox Code Playgroud)
小智 5
在我删除 Web.config 上的这一行(在根文件夹中)之前,我的 Startup.cs 不会运行
<add key="owin:AutomaticAppStartup" value="false" />
Run Code Online (Sandbox Code Playgroud)