The previous introduction to the Nancy configuration and get base operation to come down to the post operation on Nancy.
The first step is to design the main interface, with the login interface as an example: login.cshtml
The path is:
After designing the page, set the pointing path and the related post operation in the previous modules class, as shown in:
<Body> <DivID= "Login"> <formAction= " /Home"Method= "POST"ID= "Form1"> <DivID= "Bigimg"> <imgsrc= "Images/bigimg.jpg" /> </Div> <DivID= "Loginbox"> <DivID= "Title-login">Login</Div> <aID= "Title-register"href="#">Registered account ></a> <DivID= "Forinput">Account Number:<BR> <P> <inputID= "User"name= "UserId"TabIndex= "1"type= "text"> <spanID= "Error_userid"></span> </P>Password:<ahref="#"style= "color: #00A1D6; float:right; *margin-top: -10px; display:block;">Forgot my password?</a> <BR> <P> <inputID= "Password"type= "Password"TabIndex= "2"name= "Userpwd"> <spanID= "Error_userpwd"></span> </P>Verification Code:<BR> <P> <inputID= "Code"name= "Verifity"TabIndex= "3"type= "text"> <span> <imgID= "Verifity"onclick= "src = '/createcode? ' +math.random () "src= "/createcode"style= "Vertical-align:middle;"> </span> <spanID= "Error_code"style= "MARGIN-LEFT:18PX;"></span> </P> </Div> <inputtype= "Submit"ID= "Submit"value= "Login"onclick= "return Checklogin ()"> </Div> </form> </Div></Body>
View Code
1 #regionLogin--form verification, successfully jump to main page2 3post[" /Home"] = P = =4 {5 //Get Form6 varuser =Sqlhelperserver.todbvalue (request.form);7 //Verify that the user name exists8 if(!Userservice.isuserid (User.userid))9 {Ten return "<script>alert (' User name does not exist! '); location.href= '/' </script>"; One } A //Verify password input is correct - if(Userservice.getuserpwd (user.userid)! =user.userpwd) - { the return "<script>alert (' Password wrong! '); location.href= '/' </script>"; - } - if(User.verifity! = session["Code"]. ToString ()) - { + return "<script>alert (' Verification code Error! '); location.href= '/' </script>"; - } +Viewbag.userid =User.userid; ADataTable dt =Userservice.getuser (); at returnview["user/home.cshtml", dt. Rows]; - }; - - #endregionView Code
Where about Sqlhelperserver is the public class for database operations, where Todbvalue is the form data that gets the page's incoming all HTML, where the label must have a "name" attribute.
Public Static Object Todbvalue (Thisobject value) { returnnull ? DBNull.Value:value; }
View Code
At this point, the post operation is complete!
It also involves referring to external JS and external CSS files, this common MVC is not the same, but also after quite a long study to find out, and finally found very simple, as long as the modules to create a new class, as follows:
Public classBootstrapper:defaultnancybootstrapper {protected Override voidconfigureconventions (nancyconventions nancyconventions) {Base. Configureconventions (nancyconventions); NancyConventions.StaticContentsConventions.Clear (); NANCYCONVENTIONS.STATICCONTENTSCONVENTIONS.ADD (Staticcontentconventionbuilder.adddirectory ("CSS","/css")); NANCYCONVENTIONS.STATICCONTENTSCONVENTIONS.ADD (Staticcontentconventionbuilder.adddirectory ("JS","/script")); NANCYCONVENTIONS.STATICCONTENTSCONVENTIONS.ADD (Staticcontentconventionbuilder.adddirectory ("Images","/images")); }}View Code
After that, we will explain the operation of the Verification Code acquisition and the session, as well as other similar to MVC, no much change.
Concise MVC thought Framework--nancy (POST operation with external reference CSS and JS)