【问题标题】:Kept being directed to HTTP Error 404.13 page despite configuration set in Web.Config尽管在 Web.Config 中设置了配置,但仍被定向到 HTTP 错误 404.13 页面
【发布时间】:2020-07-28 16:08:33
【问题描述】:

面临的错误:HTTP 错误 404.13 - 未找到/请求过滤模块配置为拒绝超过请求内容长度的请求

编辑:决定改写和整理我的问题。

每当我通过 FileUpload Control 提交文件时,我都会遇到此错误。我想将文件大小设置为 5MB,如果选择的文件超过 5MB,它将显示错误消息(例如文件超出文件限制,请重试)。以下是我的代码。

**

WebPage1.aspx.cs

**

    protected void Button1_Click(object sender, EventArgs e)
    {
        string gen = "Pending";
        //This portion is for storing files in database
        FileInfo fi = new FileInfo(FileUpload1.FileName);
        byte[] documentContent = FileUpload1.FileBytes;
        string name = fi.Name;
        //string extn = fi.Extension;
        string filextn = Path.GetExtension(FileUpload1.FileName);

        //This portion is for storing files in folder
        string filepath = Path.GetExtension(FileUpload1.FileName);
        if (filepath.ToLower() != ".pdf" && filepath.ToLower() != ".png" && filepath.ToLower() != ".gif" && filepath.ToLower() != ".zip")
        {
            lblmessage.Text = "Only pdf, png and gif file are accepted";
        }
        else
        {
            //int filesize = FileUpload1.PostedFile.ContentLength;
            if (FileUpload1.PostedFile.ContentLength > 5242880)
            {
                //lblmessage.Text = "Maximum size (5MB) exceeded";
                Response.Redirect("Error.aspx");
            }
        }
        

Web.Config

  • 据我了解,maxRequestLength 以千字节 (KB) 为单位

      <httpRuntime targetFramework="4.7.2" maxRequestLength="5000"  />
    
  • 并且,maxAllowedContentLength 以 BYTES 为单位衡量

        <requestFiltering>
      <requestLimits maxAllowedContentLength="5242880" />
    </requestFiltering>
    

我尝试过的其他方法

  • 我在配置编辑器中配置了maxRequestLength;还是失败了。

信用:https://weblog.west-wind.com/posts/2016/apr/06/configuring-aspnet-and-iis-request-length-for-post-data https://hoststud.com/resources/how-to-increase-the-maximum-upload-file-size-in-iis.422/

【问题讨论】:

  • 您的问题是“为什么当我阻止通过配置访问我的代码时,我的代码没有被执行”?
  • @AlexeiLevenkov 你可以这样写。抱歉,如果我的问题不是很清楚,我会努力改进。
  • 您的客户端代码不能超过服务器限制。
  • 嗨@jdweng,我应该如何从这里改进/修改我的代码?我在我的 web.config 文件中设置了 5MB 的限制,并在我的客户端代码中添加了一个逻辑 (PostedFile.ContentLength)
  • 你设置了服务器限制吗?

标签: c# asp.net web-config maxrequestlength


【解决方案1】:

尝试以下配置

Web.config:

    <configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.7.2"/>
    <httpRuntime targetFramework="4.7.2" maxRequestLength="1048576" />
  </system.web>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
  </system.webServer>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>

</configuration>

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Sample_File_Upload.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <p>
            &nbsp;</p>
        <p>
            <asp:Button ID="UploadFile" runat="server" OnClick="UploadFile_Click" Text="Upload File" />
        </p>
        <asp:Label ID="message" runat="server"></asp:Label>
    </form>
</body>
</html>

WebForm1.aspx.cs

using System;

namespace Sample_File_Upload
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UploadFile_Click(object sender, EventArgs e)
        {
            if(FileUpload1.PostedFile.ContentLength > 5242880)
            {
                message.Text = "Maximum size allowed is 5MB.";
            }
            else
            {
                message.Text = "File uploaded successfully";
            }
        }
    }
}

【讨论】:

  • 你好,这里设置的长度是1GB吧?我想设置为 5MB。
  • 如果您将其设置为 5MB 并且文件大小大于 5MB,则请求将在处理之前失败。但是在您的情况下,如果文件大小大于 5MB,则需要显示验证消息。这样我们就可以将配置设置为允许大于 5MB 的文件,然后在逻辑中对其进行验证。
  • 我明白了。那么我应该如何在验证文件大小的逻辑上改进我的 WebForm1.aspx.cs 呢?我已将我的代码附在我的问题中。
  • 您的代码应该可以正常工作。只需在配置文件中添加这些设置
  • 嗨。我已将我的代码修改为您上面的 web.config 代码。在我的网络表单中,尽管为其添加了逻辑,但它并没有验证他们的文件。你能帮帮我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-21
  • 2021-04-13
  • 2020-10-13
  • 2016-08-01
  • 1970-01-01
  • 2018-05-04
  • 2014-08-06
相关资源
最近更新 更多