コード例 #1
0
        public override void OnLoad(ContainerBuilder builder)
        {
            HttpConfiguration config = GlobalConfiguration.Configuration;

            GlobalConfiguration.Configure(WebApiConfig.Register);

            SwaggerConfig.Register();

            builder.RegisterApiControllers(GetWebEntryAssembly()).InstancePerDependency();
            builder.RegisterWebApiFilterProvider(config);

            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
コード例 #2
0
        public void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration();

            // Web API configuration and services
            config.Services.Replace(typeof(IExceptionHandler), new MyExceptionHandler());

            SwaggerConfig.Register(config);

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                "DefaultApi",
                "api/{controller}/{id}",
                new { id = RouteParameter.Optional }
                );

            app.UseErrorPage()
            .UseWelcomePage("/")
            .Use((ctx, next) =>
            {
                var msg = "故意引發例外";
                Console.WriteLine(msg);
                throw new Exception(msg);
            })
            .UseWebApi(config)
            ;

            //app.Use(async (ctx, next) =>
            //        {
            //            try
            //            {
            //                await next();
            //            }
            //            catch (Exception ex)
            //            {
            //                try
            //                {
            //                    this.ErrorHandle(ex, ctx);
            //                }
            //                catch (Exception exception)
            //                {
            //                    Console.WriteLine(exception.Message);
            //                    throw;
            //                }
            //            }
            //        })
            //   .Use((ctx, next) =>
            //        {
            //            var msg = "故意引發例外";
            //            Console.WriteLine(msg);
            //            throw new Exception(msg);
            //        })
            //   .UseWebApi(config);

            //app.Use<ErrorHandler>()
            //   .Use((ctx, next) =>
            //        {
            //            var msg = "故意引發例外";
            //            Console.WriteLine(msg);
            //            throw new Exception(msg);
            //        })
            //   .UseWebApi(config);

            //app.Use<ErrorHandlerOwinMiddleware>();
            //app.Use((ctx, next) =>
            //        {
            //            var msg = "故意引發例外";
            //            Console.WriteLine(msg);
            //            throw new Exception(msg);
            //        });
            //app.UseWebApi(config);
        }