ASP.NET Core Skills

Open static pages

Startup.Configure:

app.UseStaticFiles();

Set Home

Startup.Configure:

DefaultFilesOptions defaultFilesOptions = new efaultFilesOptions();

defaultFilesOptions.DefaultFileNames.Clear();

defaultFilesOptions.DefaultFileNames.Add("index.html");

app.UseDefaultFiles(defaultFilesOptions);

IMPORTANT, the code is in order:

UseDefaultFiles-UseStaticFiles-UseMvc

Summary:

Startup.Configure:

DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();

defaultFilesOptions.DefaultFileNames.Clear();

defaultFilesOptions.DefaultFileNames.Add("index.html");

  app.UseDefaultFiles(defaultFilesOptions);     

app.UseStaticFiles();     

app.UseMvc();

Cross-domain

Startup.Configure:

app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());

Json keep the original case

Startup.ConfigureServices:

services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

Adding MIME

Startup.Configure:

        var provider = new FileExtensionContentTypeProvider();

            provider.Mappings[".osm"] = "application/octet-stream";

            app.UseStaticFiles(new StaticFileOptions()

          {             

            ContentTypeProvider = provider

          });

Lifting large file upload limit (check verification, anti-attack)

Program.BuildWebHost:

        public static IWebHost BuildWebHost(string[] args) =>            WebHost

.CreateDefaultBuilder(args)

.UseStartup<Startup>()

.UseKestrel (

options =>            {               

// All the controller does not limit the size of the body of the post

                options.Limits.MaxRequestBufferSize = long.MaxValue;

                options.Limits.MaxRequestBodySize = long.MaxValue;

                // set timeout

                options.Limits.KeepAliveTimeout = TimeSpan.MaxValue;

                options.Limits.RequestHeadersTimeout = TimeSpan.MaxValue;

            }).Build();

Startup.ConfigureServices:

            // large file uploads           

services.Configure<FormOptions>(options =>

          { 

                options.ValueLengthLimit = int.MaxValue;

              options.BufferBodyLengthLimit = long.MaxValue; 

              options.MultipartBoundaryLengthLimit = int.MaxValue;

              options.MultipartBodyLengthLimit = long.MaxValue;

          });

Web.config (If the above code does not work, then another supplementary profile)

<?xml version="1.0" encoding="utf-8"?>

<configuration>

  <location>

    <system.webServer>

    <security> 

      <requestFiltering>   

    <requestLimits maxAllowedContentLength="1073741822" /> 

    </requestFiltering>

    </security> 

  </system.webServer> 

</location></configuration>

Reproduced in: https: //www.jianshu.com/p/2c46d5af9eec

Guess you like

Origin blog.csdn.net/weixin_34375233/article/details/91071088