Example of a message book developed in the ASP. net mvc Framework (1)

Source: Internet
Author: User

I have just learned the MVC Framework for a few days and I have tried to make a small example of a message book.Program, A preliminary understanding of the functions of the MVC framework. This example shows several useful things in the MVC framework.

1. handleerrorattribute (MVC Framework built-in page error handling filter)

This built-in error handling function is very simple. You can only collect error information when an error occurs on the page and display a friendly view of the error prompt (which must be in the web. configure whether to enable the customerrors node with the friendly error message in congfig ). When you create a web application using an ASP. net mvc Framework template, a view of \ shared \ error. aspx is automatically generated. However, this default error handling function does not implement error logging. Therefore, we can inherit handleerrorattribute to implement our own myhandleerror.

Myhandleerror
1 Public   Class Myhandleerror: handleerrorattribute
2 {
3 // Rewrite Error Handling Method
4 Public   Override   Void Onexception (exceptioncontext filtercontext)
5 {
6 // Record error logs
7 This . Addlog (filtercontext );
8 // Call the error handling function of the base class
9 Base . Onexception (filtercontext );
10 }
11
12 Public   Void Addlog (exceptioncontext filtercontext)
13 {
14 Directoryinfo objdirectory =   New Directoryinfo (filtercontext. httpcontext. server. mappath ( " ~ /Log " ));
15 If ( ! Objdirectory. exists)
16 {
17Objdirectory. Create ();
18}
19 String _ Logfile = Datetime. Now. tostring ( " Yyyymm " ) +   " . Log " ;
20 String _ Logfullname = Path. Combine (objdirectory. fullname, _ logfile );
21 If ( ! File. exists (_ logfullname ))
22 {
23Filestream FS=File. Create (_ logfullname );
24FS. Close ();
25}
26
27 Using (Streamwriter SW =   New Streamwriter ( New Filestream (_ logfullname, filemode. append, fileaccess. Write )))
28 {
29 Sw. writeline (datetime. Now. tostring () +   " \ T "   + Filtercontext. Exception. Message );
30 Sw. writeline ( " Stacktrace: "   + Filtercontext. Exception. stacktrace );
31 }
32 }
33 }
34

Of course, we can also enhance the processing functions of this part, such as formatting error information into an XML file or database, in this way, we can analyze and process error logs.

2. imodelbinder (interface for complicated built-in model bindings)

You can use this interface when submitting a form.CodeTo receive the data we submitted, such:

Public actionresult usersave (User objuser)

This interface is equivalent to implementing a conversion from form data to business object data. This makes the Controller code more concise and clearer.

Userbinder
Public   Class Userbinder: imodelbinder
{
Public Modelbinderresult bindmodel (modelbindingcontext bindingcontext)
{
User objuser =   New User ();
VaR form = Bindingcontext. httpcontext. Request. form;

Objuser. userid = Convert. toint32 (Form [ " Userid " ]);
Objuser. Username = Form [ " Username " ];
Objuser. Email = Form [ " Email " ];
Objuser. Password = Formsauthentication. hashpasswordforstoringinconfigfile (Form [ " Password " ], " Sha1 " );
Objuser. createddate = Datetime. now;

Modelbinderresult objr= NewModelbinderresult (objuser );
ReturnObjr;
}
}

To use this binder, We need to register it first. For more details about how to register and use it, see the content of this article.

3. html. dropdownlist (Auxiliary method for generating select)

An important parameter of this method is selectlist, which is equivalent to the dropdownlist data source. We can specify the value and text of this dropdownlist and specify the selected items, such:

Define a selectlist
Protected Selectlist pagearray
{
Get
{
Arraylist arr =   New Arraylist ();
For ( Int I =   1 ; I <= Pagecount; I ++ )
{
Arr. Add ( New {Text =   String . Format ( " Page {0} " , I), value = I });
}
Return   New Selectlist (ARR, " Value " , " Text " , This . Pageindex );
}
}

We can reference <% = html. dropdownlist ("page", this. pagearray) %>

All the above Code is based on ASP. net mvc beta version. Currently, the sample code is not complete, and you do not have the permission to determine the content. You will continue to improve it later.

Click here to download the sample code.

 

 

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.