Implementation of a non-httpmodule MVC Framework Based on ASP. net2.0

Source: Internet
Author: User

In the ASP. net mvc Framework, address blocking is used. Although it is easy to use, it is too large to be installed and configuration is troublesome. This article passesCodePractice: implement a simple MVC framework under the ASP. net2.0 framework. The difficulty of constructing the MVC framework lies in the separation between the Controller and the view and the convenient data transmission after the separation. To keep the code concise, use the ashx file as the controller and the ASPX page as the view.

It is difficult to put the project file up, but the following is a simple description. The project is a vs2008 project with a size of 15 kb.
: Dotnetmvc.rar

First, construct a controller base class.

 

Controller class
/* *
* Author: yurow
* Http://birdshover.cnblogs.com
* Description:
*
* History: created by yurow 2009-9-20 7:30:04
  */

UsingSystem. Web;
UsingSystem. Web. Services;

Namespace Dotnetmvc. MVC {
///   <Summary>
/// Controller
///   </Summary>
[WebService (namespace =   " Http://tempuri.org/ " )]
[Webservicebinding (conformsto = Wsiprofiles. basicprofile1_1)]
Public   Abstract   Class Controller < T, K > : Ihttphandler {
///   <Summary>
/// Current request
///   </Summary>
Protected Myrequest request;
///   <Summary>
/// Output
///   </Summary>
Protected Httpresponse response;
///   <Summary>
/// Data returned to the view page
///   </Summary>
Protected Mvcviewdata < T, K > Viewdata;
///   <Summary>
/// Controller name
///   </Summary>
Private   String Controllername;
///   <Summary>
/// Controller Operation Method
///   </Summary>
Public   Abstract   Void Action ();
///   <Summary>
/// Execute request
///   </Summary>
///   <Param name = "context"> </param>
Public   Void Processrequest (httpcontext context ){
Request = Context. request;
Response = Context. response;
// Here we can use the Reflection Method to perform the operation with parameters. to simplify the operation, we have removed this part.
// Methodinfo method = This. GetType (). getmethod ("action", new type [0]);
// If (Method = NULL ){
// Throw new notimplementedexception ("not implemented! ");
// }
// Object Data = method. Invoke (this, null) as object;

Viewdata =   New Mvcviewdata < T, K > ();
Action ();
Context. Items. Add ( " Mvcviewdata " , Viewdata );
Context. server. Transfer ( " ~ /View/ "   + Controllername +   " . Aspx " , False );
}
///   <Summary>
/// Control name. The default value is "View" and "Controller name ".
/// For example, in a login. ashx request, the view/login. ASPX page is called by default as the display page.
/// When loginok is set, view/loginok. aspx is called.
///   </Summary>
Protected   String Controllername {
Get {
If ( String . Isnullorempty (controllername ))
Return   This . GetType (). Name;
Return Controllername;
}
Set {
Controllername = Value;
}
}

Public BoolIsreusable {
Get{
Return False;
}
}
}
}

The Controller calls the ASPX page in the processrequest method, which sets a virtual method action to be reloaded in a specific ashx file.

The following is how to write the default. ashx. CS file:

Default
Sing dotnetmvc. MVC;

Namespace Dotnetmvc {
///   <Summary>
/// $ Codebehindclassname $ abstract description
///   </Summary>
Public   Class Default: Controller < String , String > {
Public   Override   Void Action (){

}
}
}

In the controller, there are two important things: one is to pass the View data, and the other is to display the view (through the controllername attribute)

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.