首页 > 解决方案 > .NET Core 3.1 MVC Web 应用程序中的 413 错误

问题描述

我有一个 .Net Core MVC 3.1 Web 应用程序,我必须在其中上传一个文件,并且我希望该文件的最大大小为 100MB。我正在尝试使用 app.UseExceptionHandler 显示错误,但它不起作用。我什至使用了一个自定义中间件来尝试查看请求的去向,而它没有去那里。

启动.cs

//app.UseExceptionHandler("/Errores"); <-- Tryed this and all the ways with useStatusCode...
        //if (env.IsDevelopment())
        //{
        //    app.UseDeveloperExceptionPage();
        //}
        //else
        //{
        //    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        //    app.UseHsts();
        //}

        app.Use(async (context, next) =>
        {
            //try
            //{
                await next(); <-- It doesn't even reach here
            //}
            //catch { }
            if (context.Response.StatusCode == 413)
            {
                context.Request.Path = "/Error/404";
                await next();
            }
        });

        app.UseHttpsRedirection();
        app.UseFileServer();
        app.UseCookiePolicy();

        app.UseRouting();
        app.UseSession();

        app.UseAuthentication();

        // Add MVC to the request pipeline.
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Login}/{action=Index}/{id?}");

            endpoints.MapControllerRoute(
                name: "Errores",
                pattern: "Errores/{action}",
                defaults: new { controller = "Errores" });
        });

413.1 错误

控制器是这样的

    public ActionResult PostAyuda([FromForm] AyudaRequest dataRequest)
    {

有什么帮助吗?

标签: c#asp.net-coreiismiddleware

解决方案


推荐阅读