In the ASP.net MVC framework is the use of address interception, although very useful, but too large to install, configuration is also troublesome. Through code practice, this paper implements a simple MVC framework under the asp.net2.0 framework. The place where the MVC framework is difficult to build is the separation of controller and view and the easy transfer of data after separation. To keep the code simple, use the Ashx file as the controller, using the ASPX page as the view.
It is difficult to speak, put the project file up, and the following is a simple description. The project is a VS2008 project with a size of 15K.
Download Address:Dotnetmvc.rar
First, build a controller base class.
The following are the referenced contents: Controller class -->/** * Author:yurow * http://birdshover.cnblogs.com * Description: * * History:created by Yurow 2009-9-20 7:30:04 */ Using System.Web; Using System.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> Return to data on view page </summary> Protected Mvcviewdata<t, k> ViewData; <summary> Controller name </summary> private string controllername; <summary> Controller action 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; This can be done with parameters in a reflective way, and here to simplify, remove 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, do not set default to view page with the name of the controller For example, in a LOGIN.ASHX request, the View/login.aspx page is called as the Display page by default. When the login is successful, it is set to Loginok, and the view/loginok.aspx is called </summary> Protected string Controllername { get { if (string. IsNullOrEmpty (Controllername)) return this. GetType (). Name; return controllername; } set { Controllername = value; } } public bool IsReusable { get { return false; } } } } |
Controller calls the ASPX page in the ProcessRequest method, which sets a virtual method action to overload in a specific ashx file.
Here is the Default.ashx.cs file.
The following are the referenced contents: Default Sing Dotnetmvc.mvc;
Namespace Dotnetmvc { <summary> Summary description of $codebehindclassname $ </summary> public class default:controller<string, string> { public override void Action () {
} } } |
In controller, there are two important things that are passed to the view data, one that shows which view (by controllername this attribute)