对于过大的POST,我必须抛出什么异常? [英] What exception must I throw for a POST that is too large?

查看:90
本文介绍了对于过大的POST,我必须抛出什么异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力确保我的MVC4应用程序能够抵御慢速HTTP POST Dos攻击。我遇到了试图设置Server.ScriptTimeout的砖墙,所以现在我试图终止内容长度超过最大值的请求。我从不喜欢抛出一条毯子,所以我应该扔到这里:



  public   abstract   class  BaseController:Controller 
{
protected static int MaxContentLength = 10 ;
受保护 覆盖 void OnActionExecuting (ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Request.RequestType == POST
{
if (filterContext.HttpContext.Request .ContentLength > MaxContentLength)
{
throw 什么???
}
}
base .OnActionExecuting(filterContext);
}
}

解决方案

好吧,您可以创建自己的Exception,它将从基础Exception类继承。这将有文字和一个名称告诉用户这是坏的 - 好吧,它会告诉他出了什么问题。



创建习俗,你可以阅读本文档 [ ^ ]。



会是这样的,



 使用系统; 

// 扩展了例外基础......
< span class =code-keyword> public class FileTooLargeException:Exception {
// 默认构造函数
public FileTooLargeException(){
// 要显示的默认错误消息,
// ,例如最大文件大小为5mb。
}
// 你也可以创建自己的,接受参数和条件等
}





..然后使用该语句调用它。



但是,你也可以将一个简单的字符串传递给Exception来删除它的空白为t他的那个,



  throw   new 异常( 最大文件大小为5mb。); 





..这个会更像它。你试过吗?



当我外出时,我正在研究这个答案 - 你可以抛出System.Web中存在的HttpException。在我的第二个代码块中使用此HttpException而不是Exception,它将是 HttpException [ ^ ]。



不知何故,有一个web.config文件(因为你正在使用ASP.NET),你可以自己指定上传文件的最大大小。如果在那里指定大小,它将帮助您专注于逻辑,ASP.NET将自己处理剩余的东西。它是这样写的,



 <   configuration  >  
< < span class =code-leadattribute> system.web >
< httpRuntime maxRequestLength = xxx / >
< / system.web >
< / configuration >





..这些是您可以在此方案中使用的一些方法。 : - )


I am trying to ensure my MVC4 app is secured against the slow HTTP POST Dos attacks. I have run into a brick wall trying to set Server.ScriptTimeout, so now I'm trying to terminate requests where the content length exceeds a maximum value. I never like just throwing a blanket exception, so what should I be throwing here:

public abstract class BaseController: Controller
    {
        protected static int MaxContentLength = 10;
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Request.RequestType == "POST")
            {
                if (filterContext.HttpContext.Request.ContentLength > MaxContentLength)
                {
                    throw new what???
                }
            }
            base.OnActionExecuting(filterContext);
        }
    }

解决方案

Well, you can create your own Exception that would inherit from the base Exception class. That would have the text and a name that would tell the user that this is bad - well, it will tell him what went wrong.

For creating customs, you can read this document[^].

It will be like this,

using System;

// extends the Exception base...
public class FileTooLargeException : Exception {
   // default constructor
   public FileTooLargeException () {
      // default error message to show, 
      // such as, "Maximum file size is 5mb".
   }
   // you can create your own too, to accept parameters and conditions etc
}



.. then call it using that statement.

However, you can also pass a simple string to the Exception to remove the blank-ness of it as this one,

throw new Exception("Maximum file size is 5mb.");



.. this one would be more like it. Did you try?

While I was out, I was working on this answer - you can throw an HttpException that is present inside the System.Web. Use this HttpException instead of that Exception in my second code block and it will be the HttpException[^] in your application.

Somehow, there is a web.config file (since you're working with ASP.NET) where you can yourself specify the maximum size for the uploaded file. If you specify the size there, it will help you to focus on the logic only and the ASP.NET will take care of the remaining stuff itself. It is to be written like this,

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="xxx" />
  </system.web>
</configuration>



.. these are a few methods that you can use in this scenario. :-)


这篇关于对于过大的POST,我必须抛出什么异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆