Four authentication programming methods under ASP. net mvc, asp. netmvc

Source: Internet
Author: User
Tags see definition

Four authentication programming methods under ASP. net mvc, asp. netmvc

ASP. net mvc uses Model binding to generate a list of corresponding parameters for the target Action. However, before the actual execution of the target Action method, it is necessary to verify the bound parameters to ensure their effectiveness, we will verify the parameters as Model binding. In general, we can use four different programming modes to verify binding parameters.

1. manual verification of bound parameters when defining specific Action methods, manual verification of successfully bound parameters is undoubtedly the most direct programming method, next, we will use a simple example to demonstrate how to implement the parameter verification logic in the corresponding Action method, and respond the error information to the client without passing the verification. In an ASP. the net mvc application defines the next Person class as the verified data type. Its Name, Gender, and Age attributes indicate a Person's Name, Gender, and Age respectively. Public class Person {[DisplayName ("Name")] public string Name {get; set;} [DisplayName ("Gender")] public string Gender {get; set ;} [DisplayName ("Age")] public int? Age {get; set ;}} is defined as the next HomeController. In the Action method Index of the GET request, we create a Person object and render it as a Model in the corresponding View. Another Index method that supports POST requests has a Person-type parameter. In this Action method, we call the Validate method to verify this input parameter. If the verification succeeds (the ModeState. IsValid attribute returns True), a ContentResult with the content "input data verified" is returned. Otherwise, this parameter is displayed as a Model in the corresponding View. Public class HomeController: Controller {[HttpGet] public ActionResult Index () {return View (new Person ();} [HttpPost] public ActionResult Index (Person person Person) {Validate (person); if (! ModelState. isValid) {return View (person);} else {return Content ("input data verified") ;}} private void Validate (Person person) {if (string. isNullOrEmpty (person. name) {ModelState. addModelError ("Name", "'name' is a required field");} if (string. isNullOrEmpty (person. gender) {ModelState. addModelError ("Gender", "'gender' is a required field");} else if (! New string [] {"M", "F "}. any (g => string. compare (person. gender, g, true) = 0) {ModelState. addModelError ("Gender", "valid 'gender' must be 'M', one of 'F'");} if (null = person. age) {ModelState. addModelError ("Age", "'age' is a required field");} else if (person. age> 25 | person. age <18) {ModelState. addModelError ("Age", "valid 'age' must be between 18 and 25 years old"); }}} as shown in the code snippet above, in the Validate method, we verify the three attributes of the Person object as the parameter one by one. If the provided data is not verified, we will Call the AddModelError method of the current ModelState to convert the specified verification error message to ModelError and save it. The specific verification rules we adopt are as follows. The Name, Gender, and Age attributes of the Person object are required fields and cannot be Null (or an empty string ). Gender attribute value of Gender must be "M" (Male) or "F" (Female), and other values are invalid values. The Age attribute indicates that the Age must be between 18 and 25 years old. The following is the definition of the View corresponding to the Action Method Index. This is a strong type View with the Model type "Person". It contains a form for editing personnel information. We directly call the extension method EditorForModel of HtmlHelper <TModel> to render the Model Person object in the form in editing mode. @ Model Person

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.