Step by step, use ext js mvc and ASP. Net MVC 3 to develop a simple CMS background management system and create a controller that outputs the verification code image.

Source: Internet
Author: User

After the logon window is complete, several steps are required to complete the logon. Now let's complete the Controller that outputs the verification code image. To do this, you need to use the verifycode class, which is found online. Because it has been a long time and the source has been forgotten, I am sorry to the author.

First, create a helper directory under the project, which will be used to place helper classes, including some custom practical methods. Copy the verifycode. CS file to the directory, open the file, and add the namespace "simplecms. helper" to it. This is done to facilitate the Controller to access this class.

Add a controller named verifycodecontroller to the Controller directory (controllers). The controller code is as follows:

Usingsystem;

Usingsystem. Collections. Generic;

Usingsystem. LINQ;

Usingsystem. Web;

Usingsystem. Web. MVC;

 

Namespace simplecms. Controllers

{

Public class verifycodecontroller: Controller

{

//

// Get:/verifycode/

 

Public actionresult index ()

{

Return view ();

}

 

}

}

 

 

Because the output is an image in the file format and is not a view, you need to change the returned actionresult type to filecontentresult.

Add a reference to the verifycode class:

Usingsimplecms. helper;

 

In the verifycode class, the createverifycode method can generate a 6-bit verification code, and the createimages method can be called to return the byte array of the image, through which the image file can be generated. The Code is as follows:

Public filecontentresultindex ()

{

Verifycode v = new verifycode ();

String code = V. createverifycode (); // obtain the random code

Session ["vcode"] = code;

V. Padding = 10;

Byte [] bytes = V. createimage (CODE );

Return file (bytes, @ "image/JPEG ");

}

 

 

The keywords in the session can be defined according to your favorite name, not necessarily vcode. The padding attribute serves the same purpose as the CSS-defined patch on the page. Finally, a file consisting of byte arrays returned by createimage is returned. The file type is JPEG.

The Controller code has been completed. Now let's test whether the image can be properly displayed. You can directly press ctra + F5, or select debug in the main menu to start executing (not debugging), open the website in the browser, and modify the address to access the verifycode controller, if the result shown in Figure 8 is displayed (the verification code in the figure is different), the Controller is complete.

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.