namespace SingalRSample { public partial class Startup { public void Configuration(IAppBuilder app) { } } }
有兩種方法能讓 SystemWeb 辨識出 Startup
1.定義 OwinStartupAttribute,
例如 [assembly: OwinStartupAttribute(typeof(SingalRSample.ABC.Startup))]
2.按照他說的需要將 Startup 放在 SingalRSample 命名空間下
所以透過 OwinStartupAttribute,也可以自由定義類別名稱,只要也有 Configuration 方法就行,不一定要叫做 Startup 的,
但個人覺得按照習慣來配置會比較好,所以我還是會使用 Startup 在預設的專案命名空間下,這樣讓不同人看到專案時,也好理解。
以下是較完整的 Startup 類別 Sample code #1: (ProjectName\Startup.cs)
using System; using System.Web.Http; using Microsoft.Owin; using Microsoft.Owin.Security.OAuth; using Owin; namespace TokenWithoutIdentity { public class Startup { public void Configuration(IAppBuilder app) { HttpConfiguration config = new HttpConfiguration(); ConfigureOAuth(app); WebApiConfig.Register(config); app.UseWebApi(config); } public void ConfigureOAuth(IAppBuilder app) { OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() { AllowInsecureHttp = true, TokenEndpointPath = new PathString("/token"), //這是要認證token的url AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), //到期日多久,此範例為一天 Provider = new OAuthAuthorizationServerProvider() //實作此provider }; // Token Generation app.UseOAuthAuthorizationServer(OAuthServerOptions); app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); } } }
Sample code #2:
using Owin; using System.Web.Http; namespace OwinSelfhostSample { public class Startup { // This code configures Web API. The Startup class is specified as a type // parameter in the WebApp.Start method. public void Configuration(IAppBuilder appBuilder) { // Configure Web API for self-host. HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); appBuilder.UseWebApi(config); } } }