简体   繁体   English

C# ASP.NET MVC:如何限制通过 POST 请求传入的字符数?

[英]C# ASP.NET MVC : how do I limit the number of characters passed in through a POST request?

I am using .cshtml to send a POST request to my controller. The following is my .cshtml form.我正在使用.cshtml向我的 controller 发送POST请求。以下是我的.cshtml表单。

@using (Html.BeginForm("PostTest, "Test", FormMethod.Post))
{
    <input type="number" name="test" min="0" max="99999" />
    <button type="submit">Submit</button>
}

The number entered by the user will be sent to the controller as shown below:用户输入的号码会发送到controller,如下图:

[HttpPost]
public ActionResult PostTest(int test) 
{
     // process the data here
}

I am only expecting about 5 digits for the number that is passed in. However, if I enter a very large value with like 100 digits, the program crashes because I am using int data type.对于传入的数字,我只期望大约 5 位数字。但是,如果我输入一个非常大的值,如 100 位数字,程序会崩溃,因为我使用的是int数据类型。 Even if I change to long data type, this problem still occurs if I enter a large number.即使我改成long数据类型,输入大数还是会出现这个问题。 I think the program crashes when the argument was passed in way beyond its limit.我认为当参数以超出其限制的方式传递时程序会崩溃。

I did set a range to limit the data passed in from 0 to 99999. However, I want to prevent such a scenario in my controller action too.我确实设置了一个范围来限制从 0 到 99999 传入的数据。但是,我也想在我的 controller 操作中防止这种情况。 Is that possible?那可能吗?

How do I solve this issue?我该如何解决这个问题?

You can use string instead of int.您可以使用字符串而不是 int。 Then check if it convert into a int and if it is in the desired range .然后check if it convert into a int以及它is in the desired range try this:试试这个:

    [HttpPost]
    public ActionResult PostTest(string test)
    {
        int number = -1;
        var result = int.TryParse(test, out number);
        if (result && number >= 0 && number <= 99999)
            return Ok(number);
        else
            return BadRequest("the number is in the wrong format ... ");
    }

You can create a request data object and in this creating use Fluent Validation for this field it will give you an error after that you can send after this error BadRequest.您可以创建一个请求数据 object,在此创建过程中对此字段使用 Fluent Validation,它会给您一个错误,之后您可以在此错误 BadRequest 之后发送。

public class MyTest {
    [Range(0, 2147483646)]
    public int myproperty {get;set;}
}



    [HttpPost]
    public ActionResult PostTest(MyTest test) 
    {
         // process the data here
    }

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

相关问题 如何在C#asp.net MVC中扩展RecursionLimit的限制 - how to extend the limit of RecursionLimit in C# asp.net MVC 如何限制asp.net中XML记录中显示的字符数? - How do I limit the number of characters shown in an XML record in asp.net? 限制按钮可以被按下的次数-C#ASP.NET MVC5 JavaScript - Limit the number of times a button can be pressed - C# ASP.NET MVC5 JavaScript C# - 我的 ASP.NET MVC 页面总是返回 null 给我的 Z0ECD11C1D7A2874201D148A23BBD7A - C# - my ASP.NET MVC page always returns null to my JSON POST request asp.net c#MVC:如何在没有ViewState的情况下生活? - asp.net c# MVC: How do I live without ViewState? 如何使用c#枚举在ASP.Net MVC中创建一组多选复选框? - How do I make a group of multi-select checkboxes in ASP.Net MVC with c# enum? 如何在应用程序状态(C#ASP.net MVC)中存储列表〜数组 - How do I store List ~ Array in the Application State (C# ASP.net MVC) 如何从Html.TextBox MVC 2 asp.net C#检索值? - How do I retrieve values from Html.TextBox MVC 2 asp.net C#? 如何从ASP.NET MVC和C#中的数据库中为下拉列表选择值? - How do I select a value for a dropdownlist from a database in ASP.NET MVC and C#? 如何在C#中使用ASP.NET MVC中的控制器解决集合错误? - How do I resolve collection error with my controller in ASP.NET MVC in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM