Пример #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            Configuration.GetSection("APIDetail").Bind(APIDetailOptions);

            app.UseStatusCodePages(async context =>
            {
                var request     = context.HttpContext.Request;
                var path        = request.Path.Value;
                var queryString = request.QueryString.Value;
                var response    = context.HttpContext.Response;
                var match       = APIDetailOptions.FirstOrDefault(opt =>
                                                                  path.Equals(opt.Path, StringComparison.OrdinalIgnoreCase) &&
                                                                  QueryStringChecker.Match(opt.QueryString, queryString));
                if (match != null)
                {
                    response.StatusCode = match.StatusCode;
                    var stream          = response.Body;
                    await stream.WriteAsync(Encoding.UTF8.GetBytes(match.ResponseBody));
                }
                else
                {
                    await WriteWelcomePage(response, request);
                }
            });

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Пример #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            Configuration.GetSection("APIDetail").Bind(APIDetailOptions);

            app.UseStatusCodePages(async context =>
            {
                var request     = context.HttpContext.Request;
                var path        = request.Path.Value;
                var queryString = request.QueryString.Value;
                var method      = request.Method;
                var response    = context.HttpContext.Response;
                var match       = APIDetailOptions.FirstOrDefault(opt =>
                                                                  path.Equals(opt.Path, StringComparison.OrdinalIgnoreCase) &&
                                                                  QueryStringChecker.Match(opt.QueryString, queryString) &&
                                                                  string.Equals(opt.Method, method, StringComparison.InvariantCultureIgnoreCase));
                if (match != null)
                {
                    await Respond(response, match);
                }
            });

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapGet("/", async context =>
                {
                    await WriteWelcomePage(context.Response, context.Request);
                });
            });
        }