Let's talk about some tough things encountered during ASP. net mvc 4 Experience (1)

Source: Internet
Author: User

If you have tried ASP. net mvc 4, you will surely notice this change in accountcontroller:

 
[Authorize]
Public ClassAccountcontroller: Controller
{

//
//Get:/account/logon

[Allowanonymous]
PublicActionresult Logon ()
{
ReturnContextdependentview ();
}
}

In this sectionCodeThe difference between them and MVC 3 is that the authorize filter is added to all actions in accountcontroller, and the action that allows direct access is added with allowanonymous. This is not to mention. Next, rabbit paper encountered a tough problem,

All of us have a powerful function in ASP. net mvc 3.0, that is, remote. You can remotely interact with the database to verify the model. For example, you can verify whether the user name is repeated during registration. I don't need to talk about it anymore. I can check it online. Paste the code here:

[Remote ("Checkusernameexists","Account", Errormessage ="Sorry! In the registered members the name already exists. Please enter a different name.")]
[Display (name ="User Name")]
Public StringUsername {Get;Set;}

 

[Httpget] // you can only use get !!! Public actionresult checkusernameexists (string username) // remotely verify whether username is repeated {dataentities EF = new dataentities (); // instantiate EF var itemtitle = EF. users. where (C => C. username = username); bool exists = itemtitle. count () <1 = false; return JSON (! Exists, jsonrequestbehavior. allowget );}

Both of them are very good and powerful, but when they are together, it's time to get the remote. I checked several times and found no problem. Later I thought of the reason why all the actions in accountcontroller in MVC 4 were added with authorize. therefore, allowanonymous is added to the preceding checkusernameexists action. finally.

In programming, there will always be such a tough thing, but as long as you stick to it, I believe it will soon not hurt.

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.