Well, before we get to the core content, the next thing to do is intercept the user's request and turn the request into a sandbox.
Here we are going to complete the interception and forwarding of the request by implementing a HttpModule class. Create a new Huberhttpmodule class and inherit the IHttpModule. Now we only deal with the Application_BeginRequest event for the time being.
Get the request and response objects first
HttpApplication application = sender as HttpApplication; HttpResponse respond = Application. Response; HttpRequest request = Application. Request;
Next get the URL of the current request:
String url = Request. Url.AbsolutePath.ToString ();
We made a pact that all modules are placed in the plugins directory, that is, we need to determine whether the current URL begins with "/plugins/", and then determine whether it is a static file (typically, our action name is not allowed to include ".") ).
Next, define a urlpathentity class that converts the URL to a Urlpathentity instance object, which is used to store the URL's plug-in name, plug-in version, Controller, action.
public class urlpathentity {// <summary> ////plug-in name/// </summary> public string pluginname {get; set;} <summary> /////plug-in Version// </summary> public int pluginversion {get; set;} <summary> /////Controller name (including area)/// </summary> public string Controller {get; set;} <summary>////action name/// </summary> Public string Action {get; set;} }
Here is the URL conversion method:
<summary>url parse to Object///</summary>//<param name= "url" ></param>//<p Aram Name= "Isplugin" > is plug-in </param>//<returns></returns> public static urlpathentity Geturlpathentity (string URL, bool isplugin) {urlpathentity result = null; var matchs = URL. Split (new char[] {'/'}, Stringsplitoptions.removeemptyentries); if (isplugin) {//var matchs = pluginrgx.matches (URL); if (matchs! = null && matchs. Length > 0) {int _index = 0; result = new Urlpathentity (); Result.pluginname = matchs[_index++];//Plug-in name string _pluginversion = matchs[_index++];//Plug-in version int pluginversion =-1; Int. TryParse (_pluginversion, out pluginversion); Result.pluginversion = pluginversion; String urltemp = "/" + result.pluginname; for (; _index < matchs. Length-1;) {urltemp + = "/" + matchs[_index++]; } result.action = matchs[_index];//action name Urltemp + = "/" + result.action; Camodel controller = null;//controllers name (contains area) Urlrefaction.trygetvalue (urltemp. ToLower (), out Controller); if (Controller = null) {Result.controller = controller. Controllername.replace ("/", "."); Result.action = controller. ActionName; }}}} else {if (matchs! = null && matchs. Length > 0) {int _index = 0; result = new Urlpathentity (); Result.controller = string. empty;//controller name (including area) for (; _index < matchs. Length-1;) {Result.controller + = "." + matchs[_index++]; } Result.controller = result.controller.Substring (1); result.action = matchs[_index];//action Name}} return result; }
To get the converted object:
var urlentity = huberpluginhandle.geturlpathentity (URL. Substring (8), true);
The corresponding sandbox is found according to the object:
Sandboxdynamicloader SandBox = Huberpluginhandle.getsandbox (Urlentity.pluginname, urlentity.pluginversion);
We then package the parameters that are carried in the request:
Refrequestentity paras = new refrequestentity (); Requesthandle.fillcorrefentity (paras, request);
Okay, get ready for the work done, the last step, sandbox call:
var result = Sandbox.invokemothod (Urlentity.controller, Urlentity.action, paras);
This result is what we want, and the next thing we're going to do is return the result.
Requesthandle.resposeresult (respond, result);
At this point, our custom request pipeline even completed, which in order to prevent the entire code to bring the antipathy, omitted a lot of ancillary business, I hope you understand.
Reprint Please specify source: http://www.cnblogs.com/eric-z/p/5108862.html
Fifth. NET-based hot-swappable web framework (Interceptor---request pipeline)