예제 #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, IStudentsDbService service)  //specifying our middlewares (icindekilerin hepsi middle ware)
        {                                                                                                    //order onemli  cunku birinden birine gecmesi belli bir sirayla olmali
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            //documentation   added to middle wares
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Student API v1");
            }); //creating swagger ui automatically

            app.UseMiddleware <ExceptionMiddleware>();
            app.UseMiddleware <LoggingMiddleware>(); //registered our custom middleware

            //inline middleware
            app.Use(async(context, next) => {
                if (!context.Request.Headers.ContainsKey("Index"))
                {
                    context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                    await context.Response.WriteAsync("index is required"); //message  (async varsa await de olmali)

                    return;                                                 //short circutting  not passing to the next middleware direk response yolluyoruz ilerlemeden
                }

                string index = context.Request.Headers["Index"].ToString();
                var student  = service.GetStudentbyIndex(index);
                if (student == null)
                {
                    context.Response.StatusCode = StatusCodes.Status400BadRequest;
                    await context.Response.WriteAsync("Incorrect Index!!");
                    return;
                }
                await next(); //calls next middle ware
            });



            //api/v1/students->old version
            //api/v2/students-> new
            //keeping both endpoints active
            app.UseRouting();  // this middleware trying to find the appropriate end point but its not executing method yet

            // app.UseAuthorization(); dont have permissions for this resource

            app.UseEndpoints(endpoints =>  //responsible for creating new instance then calling the method
            {
                endpoints.MapControllers();
            });
        }
예제 #2
0
파일: Startup.cs 프로젝트: s18409/APBDT06
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentsDbService service)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Student API v1");
            });

            app.UseMiddleware <ExceptionMiddleware>();
            app.UseMiddleware <LoggingMiddleware>();


            app.Use(async(context, next) => {
                if (!context.Request.Headers.ContainsKey("Index"))
                {
                    context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                    await context.Response.WriteAsync("index is required");

                    return;
                }

                string index = context.Request.Headers["Index"].ToString();
                var student  = service.GetStudentbyIndex(index);
                if (student == null)
                {
                    context.Response.StatusCode = StatusCodes.Status400BadRequest;
                    await context.Response.WriteAsync("Incorrect Index!!");
                    return;
                }
                await next();
            });



            app.UseRouting();



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