csharp 改进的ASP.NET启动代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 改进的ASP.NET启动代码相关的知识,希望对你有一定的参考价值。

public static class StartupExtensions
    {
        public static IMvcCoreBuilder AddMvcServices(this IServiceCollection services)
        {
            return services.AddMvcCore(opts =>
            {
                opts.AddFormatterMappings();
            })
            .AddJsonOptions(j =>
            {
                j.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            })
            .AddJsonFormatters()
            .AddApiExplorer()
            .AddDataAnnotations();
        }

        public static MvcOptions AddFormatterMappings(this MvcOptions opts)
        {
            opts.FormatterMappings.SetMediaTypeMappingForFormat("xml", "application/xml");
            opts.FormatterMappings.SetMediaTypeMappingForFormat("json", "application/json");
            return opts;
        }

        public static IApplicationBuilder UseWildcardCorsPolicy(this IApplicationBuilder app)
        {
            app.UseCors(c =>
                c.AllowAnyHeader()
                .AllowAnyMethod()
                .AllowAnyOrigin()
                .AllowCredentials());
            return app;
        }
    }
public static IWebHost BuildWebHost(string[] args) =>
            new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseDefaultServiceProvider((ctx, opts) => { })
                // .ConfigureServices(s => { s.AddTransient<IConfigureOptions<KestrelServerOptions>, KestrelServerOptionsSetup>(); })
                .ConfigureAppConfiguration(args)
                .ConfigureLogging()
                .UseStartup<Startup>()
                .Build();

以上是关于csharp 改进的ASP.NET启动代码的主要内容,如果未能解决你的问题,请参考以下文章

ASP.net 5主要改进

csharp ASP.NET的助手,ASP.NET MVC应用服务器端测试

csharp TimedLogger ASP.NET CORE

csharp 表单后ASP.NET核心

csharp asp .net c#消息框

csharp ASP.NET MVC 5表单