【问题标题】:Setting view-specific maxAllowedContentLength设置视图特定的 maxAllowedContentLength
【发布时间】:2016-04-20 21:19:52
【问题描述】:

有没有办法获得特定于视图的属性 maxAllowedContentLength?

例如:假设我的 mvc 应用程序中有两个页面,我希望一个页面接受最大为 1 MB 的 Content-Length,而我希望另一页接受最大为 10 MB 的 Content-Length?

【问题讨论】:

    标签: c# asp.net asp.net-mvc iis web-config


    【解决方案1】:

    如果它们位于不同的目录中,您可以通过添加 web.config 文件为每个目录赋予其自己的 maxAllowedContentLength。在子目录中,您只需添加与基本目录不同的值。

    【讨论】:

      【解决方案2】:

      你可以实现自定义的ActionFilter

      public class ContentLengthFilter : ActionFilterAttribute
      {
          public ContentLengthFilter(int maxContentLength)
          {
              MaxContentLength = maxContentLength;
          }
          public int MaxContentLength { get; private set; }
          public override void OnActionExecuting(ActionExecutingContext filterContext)
          {
              if (filterContext.HttpContext.Request.ContentLength > MaxContentLength)
                  throw new InvalidOperationException();
              base.OnActionExecuting(filterContext);
          }
      }
      

      然后将此属性应用于特定操作

      [HttpPost]
      [ContentLengthFilter(10000)]
      public ActionResult UploadFile()
      {
          var count = Request.Files.Count;
      

      【讨论】:

        猜你喜欢
        • 2013-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-29
        • 2020-09-24
        • 1970-01-01
        • 1970-01-01
        • 2012-08-05
        相关资源
        最近更新 更多