简体   繁体   English

asp.net MVC5 C#操作/控制器的特定上载限制

[英]asp.net MVC5 C# Specific upload limit by action/controller

first English is not my first language but i will do my best ... 第一英语不是我的第一语言,但我会尽力而为...

i have spend more than one hour to figure-out how to apply maxRequestLength in a web.config for a specific view with meany route ... 我花了一个多小时来弄清楚如何在web.config中将maxRequestLength应用于具有特定路径的特定视图...

here is my routes: 这是我的路线:

/{Controller}/{Action}/{id}

/{CategoryId}/{Controller}/{Action}/{id}

I want to allow upload of 200Mb on only specific action/view and not in all the application. 我只想允许在特定操作/视图上上传200Mb,而不是在所有应用程序中都上传。 Some time, the same view have different url, like the view Item can be called by AddItem and EditItem. 有时,同一视图具有不同的url,例如视图Item可以由AddItem和EditItem调用。

i was hopping i can set this using attribute, like [AllowUpload(200)] so i have try it but this setting is evaluated before the attribute in MVC. 我希望可以使用[AllowUpload(200)]这样的属性来设置此属性,因此我已经尝试过,但是此设置在MVC中的属性之前进行评估。

what i'm trying to do is to set the max on the web.config and register a filter attribute to deny the action in the Custom Attribute. 我想做的是在web.config上设置最大值,并注册一个过滤器属性以拒绝“自定义属性”中的操作。 controller will look like this : 控制器将如下所示:

[AllowUpload(1)]
public class MyController : Controller
{

    public ActionResult Index()
    {return View();}

    [AllowUpload(200)]
    public ActionResult Upload()
    {return View();}

}

I don't know how to do this attribute and how the controller attribute will be overridden by the action attribute. 我不知道如何执行此属性,以及操作属性将如何覆盖控制器属性。

the best way i can imagine is the attribute because i will have different upload right on meany views who can have different actions, but if you have an idea, a plugin or any-thing else, please tell me. 我能想象的最好的方法是属性,因为我将在具有不同动作的卑鄙观点上拥有不同的上载权,但是如果您有想法,插件或其他任何东西,请告诉我。

tank you very mush 坦克你很糊

I have finally found how to do this. 我终于找到了如何做到这一点。

i set the max value on webconfig, and add an ActionFilterAttribute to each controller. 我在webconfig上设置最大值,并向每个控制器添加一个ActionFilterAttribute。

the only thing i need to be sure is to always check ModelState.IsValid in controller's actions. 我唯一需要确定的是始终检查控制器动作中的ModelState.IsValid。

here is the attribute code: 这是属性代码:

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)]
public sealed class UploadActionAttribute : ActionFilterAttribute
{
    public UploadActionAttribute(double maxMb = 4d)
    {
        MaxMb = maxMb;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (ConvertBytesToMegabytes(filterContext.HttpContext.Request.ContentLength) > MaxMb)
        {
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                filterContext.Result = new JsonResult() { Data = new { Success = false, MaxContentLengthExceeded = true } };
            }
            else
                filterContext.Controller.ViewData.ModelState.AddModelError("", string.Format(CSRess.MaxRequestLengh, MaxMb));
        }
        base.OnActionExecuting(filterContext);
    }

    private double MaxMb { get; set; }
    static double ConvertBytesToMegabytes(long bytes)
    {
        return (bytes / 1024f) / 1024f;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM