欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET MVC 从IHttp到页面输出的实例代码

程序员文章站 2024-02-28 13:29:40
复制代码 代码如下:mvchandler : ihttphandlervoid ihttphandler.processrequest(httpcontext httpco...

复制代码 代码如下:

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 controller, out factory);
    try
    {
        controller.execute(this.requestcontext);
    }
    finally
    {
        factory.releasecontroller(controller);
    }
}       

复制代码 代码如下:

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();
    }
}

复制代码 代码如下:

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);
}

复制代码 代码如下:

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接口   
    }
    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);
    }
}