Since the Guoxiu home page published QMVC source code, very interested, with half a month of time to learn, really feel the income is not shallow, in this statement thank you Guoxiu Big Brother's sharing!
1, light and simple, the framework of a few classes, simple, of course, the code is running fast! Simple MVC, so if you want to extend the framework, you can easily add and reduce functionality on the QMVC, that is, easier to modify and read the source code.
2, can be combined with the WebForm framework, that is, you use WebForm and MVC together in the same project run.
3, QMVC app development, QMVC app can easily achieve a number of QMVC projects into a project to run, can easily be separated from running independently, such as a website often forum, article system, news system, etc., if your program with the Qmvc app, which can easily achieve the integration of multiple systems.
This article topic: "QMVC Advanced development", so-called advanced I do not think that is not learning, do not understand the bluff is high-level! But high and primary level of knowledge, so here, I assume you have understood the basic application of QMVC, if you want to understand the QMVC of the primary application, you can refer to my article "high-performance." NET MVC Qmvc, or QMVC official website (www.wuxiu.org).
What kind of framework is best for you? Which Zhanghao undoubtedly ask, most suitable for your frame, what is the most suitable for your own? Must be tailor-made! It's like if you say Qmvc, his code is very small, the hierarchy is simple, the logic is clear and simple. You can easily expand into a framework that fits your system, based on QMVC! This article is about how to expand your MVC app framework, talk about QMVC source structure!
1, QMVC Source analysis
First of all, there are two MVC patterns in the QMVC source code, one is QMVC basic framework, one is QMVC app framework, QMVC app code is based on the QMVC basic framework, the source of the apps directory is about the QMVC app framework implementation code. Root directory is the QMVC basic framework, router for QMVC routing function, with HTTP request address analysis.
base class for all controllers of the Controllerbase class
Imvcmodel model Call interface, of course, you can also do not inherit this interface, if you want to call the model in the Qmvcrazorpage class, which can be implemented in the interface to get.
The Mvcrazorpagebase class is the base class for the view template
The core class of the Qmvccore class QMVC framework controls the supporters of QMVC operation.
The Viewresult class is the information class when the controller passes the value to the Qmvccore to invoke the razor template, and the controller takes the information to be displayed to Viewresult,qmvccore through Viewresult loading Razo R template.
Router.irouterprovider interface If you want to rewrite the QMVC routing function, which can implement the Router.irouterprovider interface implementation.
The classes in apps are similar to the above classes, and the fixed router are implemented.
If you want to rewrite the template display path rules can override the Gettemplateurl method in the Controllerbase class, which undoubtedly gives you the most control of the space to write your own frame, the original method code:
Protected virtual string Gettemplateurl (string themename,string controllername,string viewName) { return "~/" + ThemeName + "views/" + controllername + "/" + ViewName + ". cshtml";
Defaultrouter class
Using system;using system.collections.generic;using system.collections.specialized;using System.Linq;using System.reflection;using system.text;using system.text.regularexpressions;using System.Web;namespace Wuxiu. Qmvc. router{public class Defaultrouter:irouterprovider {string Defaultcontroller; String DefaultAction; routercontrollercollection controllers = new Routercontrollercollection (); Public Defaultrouter (Assembly asm, String Defaultcontroller, String defaultaction) {Defaultcontroller = Defaultcontroller; DefaultAction = DefaultAction; Controllers. loadassembly (ASM); } public controllerbase Loadurl (httpcontextbase context, String apprawurl) {string Controllernam e = Defaultcontroller; string action = DefaultAction; String Pageurl; if (Apprawurl. IndexOf ('? ')! =-1) {pageurl = Apprawurl. Substring (0, Apprawurl. IndexOf ('? ')); } else Pageurl = Apprawurl; String[] urls= utils.clearstrarrayempty (pageurl. Split ('/')); String[] Urldata; if (URLs. length>0) {controllername=urls[0]; }else Controllername=defaultcontroller; if (URLs. length>1) {action=urls[1]; if (!action. EndsWith (". Action", StringComparison.OrdinalIgnoreCase)) {return null; } action = action. Substring (0,action. Length-". Action". Length); }else action=defaultaction; if (URLs. LENGTH>2) {urldata=new string[urls. LENGTH-2]; for (int i=2;i<urls. length;i++) urldata[i-2]=urls[i]; }else urldata=new string[]{}; Type Controllertype = controllers. Getcontroller (controllername); if (controllertype! = null) {ContRollerbase controller = activator.createinstance (controllertype) as controllerbase; Controller.init (context, controllername, action, urldata); return controller; } return null; } }}
Qmvc default Router class, no regular expression implemented, actually I think this is a good thing, because the use of character analysis more accurate performance better. The Defaultrouter class implements the Irouterprovider interface, parses the HTTP request address in the Loadurl function, and invokes the method in the controller by reflection.
2, QMVC app Introduction
The Qmvc app is designed to achieve multiple project fusion solutions, but if your program is developed in accordance with the Qmvc app pattern, it's easy to integrate with another app to run in a single site. If your project is developed in accordance with the QMVC app model, which controller is inherited from the Wuxiu.QMVC.APPS.APPController base class, each application must implement the Appprovider class. The app. config file is stored in each application.
QMVC APP directory structure
APPS
APPName
Defaultviews
Controllername
Actionname.cshtml
...
App.
Controllers
Models
APPProvider.cs
Global.asax
Web. config
In the tree above, the apps directory is a must directory that contains all of the application folders in the current project, AppName is the app's folder, which includes views and the view that the app is stored in App.config,views, and app configuration information is defined in app. Config.
APPProvider.cs is a class that is implemented by all apps in the apps mode, and he inherits from the Wuxiu.QMVC.APPS.APPProvider class, which is called during QMVC initialization to initialize the app. The models and controllers files are directories for the relevant controllers and models of the current application.
QMVC App project configuration
If you build a project with the Qmvc app mode, you must modify some of the configuration files in the directory structure defined in section 6.2 to allow the ASP. Run framework to support the Qmvc app.
Web. config, add or modify the following configuration items:
<configuration> <system.web> <compilation debug= "true" targetframework= "4.5" > <assemblies> ; <add assembly= "System.Web.WebPages.Razor2, version=0.0.0.0, culture=neutral, Publickeytoken=14679ed9c77dd5f5"/ > <add assembly= "System.Web.WebPages2, version=0.0.0.0, Culture=neutral, Publickeytoken=14679ed9c77dd5f5"/&G T <add assembly= "System.Web.Razor2, version=0.0.0.0, culture=neutral, publickeytoken=14679ed9c77dd5f5"/> </a ssemblies> <buildProviders> <remove extension= ". cshtml"/> <add extension= ". Cshtml "Type=" System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor2 "/> </buildprov iders> </compilation> </system.web> <system.webServer> <modules runallmanagedmodulesforall Requests= "true"/> </system.webServer> </configuration>
Apps/web.config, add or modify the following configuration items:
<configuration> <configSections> <sectiongroup name= "System.web.webPages.razor" type= " System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor2 "> <section Name= "Host" type= "System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor2" Requirepermission= "false"/> <section name= "pages" type= " System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor2 "requirepermission=" false " /> </sectionGroup> </configSections> <system.web> </system.web> <system.web.webPages.razor> <pages pagebasetype= "Wuxiu. Qmvc. Mvcrzorpagebase "> <namespaces> <add namespace=" Wuxiu. Qmvc "/> </namespaces> </pages> </system.web.webpages.razor></ Configuration>
Apps/appname/appprovider.config
<?xml version= "1.0" encoding= "Utf-8"?> <app name= "Paste System" version= "1.0" copyright= "Bobby"
urlfolder= "Wuxiu" appproviderclass= "Wuxiu.tiebaAPP.TiebaAPPProvider" assembly= "Bin:wuxiu.tiebaAPP" enable= "true" > <appSettings> <add key= "" value= ""/> ... </appSettings> &l t;customelements> </customElements> </app>
If you create a QMVC app, you must write an app. config to describe your application information, QMVC will load the app according to your configuration, and pass your configuration information to the app.
App Node Properties
The name of the app, version number currently applied, Appproviderclass represents the implementation of Wuxiu. Qmvc. APPS. The namespace and class name of the Appprovider class used to initialize the app's class. Assembly the class library where the Appprovider is currently applied, the value has two writing styles:
1, the file colon begins, identifies the DLL file name under the app/bin/directory, such as: "File:mydll.dll".
2, Bin: The default is the program with the directory bin directory under the name of the assembly, such as: "Bin:mydll".
Note: The bin prefix is recommended, because file can be independent of its own program directory, there is a running monopoly problem, may cause the program to run the page DLL is not allowed to move and delete. Enable identifies whether the current program is available, with a value of two: TRUE or false.
The above chapters are translated from the Qmvc doumentation section of the homepage of my home about the Qmvc app chapter!
Achieve a appcontroller of your own
public class Homecontroller:wuxiu. Qmvc. APPS. appcontroller{public Wuxiu. Qmvc. ViewResult Index () { return View (); }}
Implement a Imvcmodel class
When the Mvcrazorpage object can be obtained in the model class, it will undoubtedly add the model function, he can provide some program logic more flexibly, reduce the code in the Razor template, after all, Razor is the level code for the art operation.
public class Mymodel:wuxiu. Qmvc. Iqmvcmodel {public string Loginpageurl {get {return page. Appurlstart + "/user/login.action"; }} Wuxiu. Qmvc. APPS. Apprazorpage page; public void Init (Qmvcrzorpagebase page) { this.page = (Wuxiu. Qmvc. APPS. apprazorpage) page; } }