asp.net MVC from ihttp to page Output example code _ Practical Tips

Source: Internet
Author: User
Tags httpcontext

Copy Code code as follows:

Mvchandler:ihttphandler
void Ihttphandler.processrequest (HttpContext HttpContext)
{
This. ProcessRequest (HttpContext);
}

protected virtual void ProcessRequest (HttpContext HttpContext)
{
HttpContextBase base2 = new Httpcontextwrapper (HttpContext);
This. ProcessRequest (BASE2);
}

protected internal virtual void ProcessRequest (HttpContextBase HttpContext)
{
IController Controller;
Icontrollerfactory Factory;
This. Processrequestinit (HttpContext, out of controller, out factory);
Try
{
Controller. Execute (this. RequestContext);
}
Finally
{
Factory. Releasecontroller (Controller);
}
}

Copy Code code as follows:

Controller:ControllerBase:IController
void Icontroller.execute (RequestContext requestcontext)//------>controller
{
This. Execute (RequestContext);
}

protected virtual void Execute (RequestContext requestcontext)//------>controllerbase
{
if (RequestContext = null)
{
throw new ArgumentNullException ("RequestContext");
}
if (Requestcontext.httpcontext = null)
{
throw new ArgumentException (Mvcresources.controllerbase_cannotexecutewithnullhttpcontext, "RequestContext");
}
This. Verifyexecutecalledonce ();
This. Initialize (RequestContext);
using (Scopestorage.createtransientscope ())
{
This. Executecore ();
}
}

protected override void Executecore ()//------>controller
{
This. Possiblyloadtempdata ();
Try
{
String requiredstring = this. Routedata.getrequiredstring ("action");
if (!this. Actioninvoker.invokeaction (base. ControllerContext, requiredstring))//public iactioninvoker actioninvoker {get; set;}
{
This. Handleunknownaction (requiredstring);
}
}
Finally
{
This. Possiblysavetempdata ();
}
}

Copy Code code as follows:

Controlleractioninvoker:iactioninvoker
Public virtual bool Invokeaction (ControllerContext ControllerContext, String actionname)
{
if (ControllerContext = null)
{
throw new ArgumentNullException ("ControllerContext");
}
if (string. IsNullOrEmpty (ActionName))
{
throw new ArgumentException (Mvcresources.common_nullorempty, "actionname");
}
Controllerdescriptor Controllerdescriptor = this. Getcontrollerdescriptor (ControllerContext);
Actiondescriptor Actiondescriptor = this. Findaction (ControllerContext, Controllerdescriptor, ActionName);
if (Actiondescriptor = null)
{
return false;
}
FilterInfo filters = this. Getfilters (ControllerContext, actiondescriptor);
Try
{
AuthorizationContext context = this. Invokeauthorizationfilters (ControllerContext, filters. Authorizationfilters, Actiondescriptor);
if (context. Result!= null)
{
This. Invokeactionresult (ControllerContext, context. result);
}
Else
{
if (controllerContext.Controller.ValidateRequest)
{
ValidateRequest (ControllerContext);
}
idictionary<string, object> parametervalues = this. Getparametervalues (ControllerContext, actiondescriptor);
ActionExecutedContext context2 = this. Invokeactionmethodwithfilters (ControllerContext, filters. Actionfilters, Actiondescriptor, parametervalues);
This. Invokeactionresultwithfilters (ControllerContext, filters. Resultfilters, Context2. result);
}
}
catch (ThreadAbortException)
{
Throw
}
catch (Exception Exception)
{
Exceptioncontext CONTEXT3 = this. Invokeexceptionfilters (ControllerContext, filters. Exceptionfilters, exception);
if (!CONTEXT3. exceptionhandled)
{
Throw
}
This. Invokeactionresult (ControllerContext, Context3. result);
}
return true;
}

protected virtual void Invokeactionresult (ControllerContext controllercontext, ActionResult ActionResult)
{
Actionresult.executeresult (ControllerContext);
}

Copy Code code as follows:

ActionResult
public override void Executeresult (ControllerContext context)
{
if (context = null)
{
throw new ArgumentNullException ("context");
}
if (string. IsNullOrEmpty (this. ViewName))
{
This. ViewName = context. Routedata.getrequiredstring ("action");
}
Viewengineresult result = null;
if (this. View = = null)
{
result = this.   Findview (context); Viewengineresult
This. View = result.    View; IView interface
}
TextWriter output = context. HttpContext.Response.Output;
ViewContext viewcontext = new ViewContext (context, this. View, this. ViewData, this. TempData, output);
This. View.render (viewcontext, Output);
if (Result!= null)
{
Result. Viewengine.releaseview (context, this.) View);
}
}

Related Article

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.