Example #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(c =>
             * {
             *   c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
             * });*/
            app.UseMiddleware <LoggingMiddleware>();
            app.Use(async(context, next) =>
            {
                if (!context.Request.Headers.ContainsKey("Index"))
                {
                    context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                    await context.Response.WriteAsync("Nie podadano numeru indeksu");
                    return;
                }

                string index = context.Request.Headers["Index"].ToString();
                if (!service.CheckStudent(index))
                {
                    context.Response.StatusCode = StatusCodes.Status404NotFound;
                    await context.Response.WriteAsync("Nie znaleziono studenta o podanym numerze indeksu");
                    return;
                }
                await next();
            });
            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

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