示例#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, IStudentDbService service)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseSwagger();
            app.UseSwaggerUI(config =>
            {
                config.SwaggerEndpoint("/swagger/v1/swagger.json", "Students App API");
            });
            app.UseMiddleware <LoggingMiddleware>();
            app.Use(async(context, next) =>

            {
                if (!context.Request.Headers.ContainsKey("IndexNumber"))
                {
                    context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                    await context.Response.WriteAsync("Taki numer nie istnieje");
                    return;
                }

                string index    = context.Response.Headers["IndexNumber"];
                Student student = service.GetStudent(index);
                if (student == null)
                {
                    await context.Response.WriteAsync("Taki student nie istnieje");
                    context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                    return;
                }
                await context.Response.WriteAsync(student.ToString());
                await next();
            });
            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }