[ASP. NET MVC2 series]
[ASP. NET MVC2 series] ASP. net mvc tutorial-create a Movie Database Application with ASP. net mvc within 15 minutesProgram"
[ASP. NET MVC2 series] ASP. net mvc introduction to ASP. NET MVC
[ASP. NET MVC2 series] Understand the execution process of MVC applications
[ASP. NET MVC2 series] ASP. net mvc routing Overview
[ASP. NET MVC2 series] How to create custom routing constraints in ASP. NET MVC
[ASP. NET MVC2 series] action requirements and view nature
[ASP. NET MVC2 series] Action filters and custom outputcache actionfilterattribute event occurrence order
Requirements required for creating a custom action
The method must be public.
The method cannot be a static method.
The method cannot be a method in the control base class (for example, tostring or gethashcode)
The method cannot be an extension method.
The method cannot beConstructor, Getter, setter.
The method cannot contain the ref or out parameters.
Using the nonactionattribute feature will prevent this action from being called.
Namespace Mvcviewandaction. Web. Controllers
{
[Handleerror]
Public Class Userdemocontroller: Controller
{
//
// Get:/userdemo/
// Customize a simple method
[Nonaction]
// Public sealed class nonactionattribute indicates a feature used to indicate that the Controller method is not an operation method.
Public String Displaystring ()
{
Return " This is a demo string! " ;
}
}
}
Understanding views
Compared with ASP. NET and Active Server Pages, ASP. net mvc does not contain any directly corresponding page. In the ASP. net mvc application, if you type a URL in the address bar of the browser and there is no corresponding page on the disk, the URL is mapped to Controller actions. What is most similar to page is what we call view.
The basic information is as follows:
Public Actionresult index ()
{
Return View ();
}
To explore the nature of the view, the following results are displayed:
Public Class Userdemocontroller: Controller
{
Public Rssactionresult rssshow ()
{
Return New Rssactionresult ();
}
}
We need to create an inheritanceThe rssactionresult class of actionresult is as follows:
Namespace Mvcviewandaction. Common
{
/*
* System. Web. MVC
Public abstract class actionresult
{
Protected actionresult ();
Public abstract void executeresult (controllercontext context );
} */
Public Class Rssactionresult: actionresult
{
Public Rssactionresult ()
{
}
//
Public Override Void Executeresult (controllercontext context)
{
If (Context = Null )
{
Throw New Argumentnullexception ( " Controllercontext is null! " );
}
Httpresponsebase response = Context. httpcontext. response;
RSS = New RSS ();
RSS. createsamplerss (response );
}
}
Use the custom type inherited from the system. Web. MVC. actionresult class and execteresult (controllercontext context) to enable processing of the operation method results.
Appendix: The RSS. createsamplerss (response) method in executeresult () uses httpresponsebase to process response streams.
Public Void Createsamplerss (httpresponsebase response)
{
Xmltextwriter writer = New Xmltextwriter (response. outputstream, system. Text. encoding. utf8 );
Writerssheader (writer );
For ( Int I = 0 ; I < 50 ; I ++ )
{
Writerssitem (writer, " Demo title: jasenkin " + I. tostring (), Http: // jasenkin/ , " Decription: ---> " + I. tostring ());
}
Writerssbottom (writer );
Writer. Flush ();
Writer. Close ();
Response. contentencoding = System. Text. encoding. utf8;
Response. contenttype = " Text/XML " ;
Response. cache. setcacheability (httpcacheability. Public );
Response. End ();
}
The execution result is as follows:
A typical action may receive user input, prepare appropriate response data, and then return a result type (rssactionresult in the preceding example ), the system will automatically call the executeresult (context) of this result type (rssactionresult in the preceding example) to respond to browser requests. The view is what we call (for example ).
This is just a custom class that inherits the actionresult. The executeresult () method is the key to this class.
Source code:Mvcviewandaction.rar (vs2010)