C# 未调用WebApiConfig中的.Net OWIN
我创建了一个空白项目,以便创建一个角度应用程序。 现在,我已经准备好了所有这些,我决定将WebAPI添加到这个项目中。我安装了所有必需的软件包并设置了WebApiConfig.cs文件。 然后我安装了OWIN,并创建了OWIN启动类。当我运行我的项目时,OWIN启动类被正确调用,但是WebApiConfig没有被正确调用 在过去(OWIN之前),使用Global.asax是您启动所有配置类的方式,但由于我使用OWIN而不需要Global.asax文件,因此我从未创建过它 以前有没有人遇到过这种情况,并且知道我做错了什么 更新1 我添加了一个Global.asax页面并执行了它。 我的印象是,如果您使用OWIN,您应该删除您的Global.asax文件吗 以下是Global.asax文件C# 未调用WebApiConfig中的.Net OWIN,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,我创建了一个空白项目,以便创建一个角度应用程序。 现在,我已经准备好了所有这些,我决定将WebAPI添加到这个项目中。我安装了所有必需的软件包并设置了WebApiConfig.cs文件。 然后我安装了OWIN,并创建了OWIN启动类。当我运行我的项目时,OWIN启动类被正确调用,但是WebApiConfig没有被正确调用 在过去(OWIN之前),使用Global.asax是您启动所有配置类的方式,但由于我使用OWIN而不需要Global.asax文件,因此我从未创建过它 以前有没有人遇到过这种情
public class Global : HttpApplication
{
protected void Application_Start()
{
// Add these two lines to initialize Routes and Filters:
WebApiConfig.Register(GlobalConfiguration.Configuration);
}
}
以及Startup.Config文件
public class StartupConfig
{
public static UserService<User> UserService { get; set; }
public static string PublicClientId { get; private set; }
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
static StartupConfig()
{
UserService = new UserService<User>(new UnitOfWork<DatabaseContext>(), false, true);
PublicClientId = "self";
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new OAuthProvider<User>(PublicClientId, UserService),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
}
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void Configuration(IAppBuilder app)
{
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthOptions);
// Uncomment the following lines to enable logging in with third party login providers
//app.UseMicrosoftAccountAuthentication(
// clientId: "",
// clientSecret: "");
//app.UseTwitterAuthentication(
// consumerKey: "vnaJZLYwWFbv7GBlDeMbfwAlD",
// consumerSecret: "Q1FE1hEN6prXnK2O9TYihTFyOQmcQmrZJses0rT8Au4OsDQISQ");
//app.UseFacebookAuthentication(
// appId: "",
// appSecret: "");
//app.UseGoogleAuthentication();
}
}
公共类StartupConfig
{
公共静态用户服务用户服务{get;set;}
公共静态字符串PublicClientId{get;private set;}
公共静态OAuthAuthorizationServerOptions OAuthOptions{get;private set;}
静态启动配置()
{
UserService=newuserservice(newunitofwork(),false,true);
PublicClientId=“self”;
OAuthOptions=新的OAuthAuthorizationServerOptions
{
TokenEndpointPath=新路径字符串(“/Token”),
Provider=新的OAuthProvider(PublicClientId,UserService),
AuthorizeEndpointPath=新路径字符串(“/api/Account/ExternalLogin”),
AccessTokenExpireTimeSpan=TimeSpan.FromDays(14),
AllowInsecureHttp=true
};
}
//有关配置身份验证的详细信息,请访问http://go.microsoft.com/fwlink/?LinkId=301864
公共无效配置(IAppBuilder应用程序)
{
//使应用程序能够使用cookie存储登录用户的信息
//以及使用cookie临时存储用户登录第三方登录提供商的信息
app.UseCookieAuthentication(新的CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
//使应用程序能够使用承载令牌对用户进行身份验证
应用程序使用OAuthBealerTokens(OAuthOptions);
//取消注释以下行以启用使用第三方登录提供程序登录
//app.UseMicrosoftAccountAuthentication(
//客户ID:“,
//客户机密:);
//app.UseTwitterAuthentication(
//消费者:“Vnajzlywwbv7gbldembwwald”,
//消费者信用:“Q1FE1hEN6prXnK2O9TYihTFyOQmcQmrZJses0rT8Au4OsDQISQ”);
//app.UseFacebookAuthentication(
//appId:“”,
//appSecret:”;
//app.UseGoogleAuthentication();
}
}
更新2
我的startup类现在看起来像这样:
public class StartupConfig
{
public static UserService<User> UserService { get; set; }
public static string PublicClientId { get; private set; }
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
static StartupConfig()
{
UserService = new UserService<User>(new UnitOfWork<DatabaseContext>(), false, true);
PublicClientId = "self";
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new OAuthProvider<User>(PublicClientId, UserService),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
}
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void Configuration(IAppBuilder app)
{
//var config = new HttpConfiguration();
//// Set up our configuration
//WebApiConfig.Register(config);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthOptions);
// Uncomment the following lines to enable logging in with third party login providers
//app.UseMicrosoftAccountAuthentication(
// clientId: "",
// clientSecret: "");
//app.UseTwitterAuthentication(
// consumerKey: "vnaJZLYwWFbv7GBlDeMbfwAlD",
// consumerSecret: "Q1FE1hEN6prXnK2O9TYihTFyOQmcQmrZJses0rT8Au4OsDQISQ");
//app.UseFacebookAuthentication(
// appId: "",
// appSecret: "");
//app.UseGoogleAuthentication();
}
}
公共类StartupConfig
{
公共静态用户服务用户服务{get;set;}
公共静态字符串PublicClientId{get;private set;}
公共静态OAuthAuthorizationServerOptions OAuthOptions{get;private set;}
静态启动配置()
{
UserService=newuserservice(newunitofwork(),false,true);
PublicClientId=“self”;
OAuthOptions=新的OAuthAuthorizationServerOptions
{
TokenEndpointPath=新路径字符串(“/Token”),
Provider=新的OAuthProvider(PublicClientId,UserService),
AuthorizeEndpointPath=新路径字符串(“/api/Account/ExternalLogin”),
AccessTokenExpireTimeSpan=TimeSpan.FromDays(14),
AllowInsecureHttp=true
};
}
//有关配置身份验证的详细信息,请访问http://go.microsoft.com/fwlink/?LinkId=301864
公共无效配置(IAppBuilder应用程序)
{
//var config=新的HttpConfiguration();
////设置我们的配置
//WebApiConfig.Register(配置);
//使应用程序能够使用cookie存储登录用户的信息
//以及使用cookie临时存储用户登录第三方登录提供商的信息
app.UseCookieAuthentication(新的CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
//使应用程序能够使用承载令牌对用户进行身份验证
应用程序使用OAuthBealerTokens(OAuthOptions);
//取消注释以下行以启用使用第三方登录提供程序登录
//app.UseMicrosoftAccountAuthentication(
//客户ID:“,
//客户机密:);
//app.UseTwitterAuthentication(
//消费者:“Vnajzlywwbv7gbldembwwald”,
//消费者信用:“Q1FE1hEN6prXnK2O9TYihTFyOQmcQmrZJses0rT8Au4OsDQISQ”);
//app.UseFacebookAuthentication(
//appId:“”,
//appSecret:”;
//app.UseGoogleAuthentication();
}
}
如果我取消对WebApiConfig行的注释,那么启动类永远不会执行。
知道为什么吗?当然,如果您使用Owin,您可以删除Global.asax文件 在Owin Startup.cs中,您必须注册WebApiConfig
public class Startup
{
public void Configuration(IAppBuilder app)
{
...
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
config.Filters.Add(new WebApiAuthorizeAttribute());
...
}
...
}
您需要在startup类中调用app.UseWebApi,并传入您要使用的配置。您还需要在那里调用WebApiConfig的Register方法。这在精简应用程序中的表现示例如下: 您可以有一个如下所示的OWIN启动类:
// Tell OWIN to start with this
[assembly: OwinStartup(typeof(MyWebApi.Startup))]
namespace MyWebApi
{
public class Startup
{
/// <summary>
/// This method gets called automatically by OWIN when the application starts, it will pass in the IAppBuilder instance.
/// The WebApi is registered here and one of the built in shortcuts for using the WebApi is called to initialise it.
/// </summary>
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
}
}
}
namespace MyWebApi
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
发布来自您的global asax.cs和startup.cs的代码-没有这些,您将得到的只是猜测