Asp .Net Core -Startup Class and the Service Configuration

Ref: https://code-maze.com/aspnetcore-webapi-best-practices/

In the Startup class, there are two methods: the ConfigureServices method for registering the services and the Configure method for adding the middleware components to the application’s pipeline.



 public static class ServiceExtensions
    {
        public static void ConfigureCors(this IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                    builder => builder.AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
            });
        }

        public static void ConfigureIISIntegration(this IServiceCollection services)
        {
            services.Configure<IISOptions>(options =>
            {

            });
        }
        public static void ConfigureMVC(this IServiceCollection services)
        {
            services.AddMvc(options =>
            {
                // Add XML Content Negotiation
                options.RespectBrowserAcceptHeader = true;
                options.InputFormatters.Add(new XmlSerializerInputFormatter());
                options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
            });
        }

        public static void ConfigureSwagger(this IServiceCollection services)
        {
            //Swagger Configuration 
            services.ConfigureSwaggerGen(options =>
            {
                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetEntryAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine("./wwwroot/XmlDocument/", xmlFile);
                if (!string.IsNullOrEmpty(xmlPath))
                    options.IncludeXmlComments(xmlPath);
                options.DescribeAllEnumsAsStrings();

            });
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "sample", Version = "v1" });
            });
        }

    }

Startup Class:

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.ConfigureMVC();
            services.ConfigureSwagger();
            services.ConfigureCors();
            services.ConfigureIISIntegration();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseCors("CorsPolicy");
            app.UseStaticFiles(); // For the wwwroot folder            
            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();
            //Custom Swagger ui setting
            app.UseSwaggerUI(o =>
            {
                o.SwaggerEndpoint("../swagger/v1/swagger.json", "sample");
                o.InjectStylesheet("../css/Swagger-ui.css");
            });
            //swagger default url setting 
            var option = new RewriteOptions();
            option.AddRedirect("^$", "swagger");
            app.UseRewriter(option);
            app.UseMvc();
        }

    }

Comments

  1. try this C# Interview Questions...

    http://net-informations.com/faq/default.htm

    ReplyDelete

Post a Comment

Popular posts from this blog

Chat Bot

Entity Framework

Microsoft Enterprise Library-Data Access Application Block for for .Net Core & .Net Standard