下面看看MvcHandler做了些什么

1 把HttpContext包装成HttpContextWrapper

2 获取匹配的ControllerFactory

3 ControllerFactory.CreateController获取匹配的Controller

4 调用Controller的Execute方法,调用Controller的ExecuteCore方法

5 获得action的名字,获得ControllerActionInvoker

6 调用ControllerActionInvoker.InvokeAction

public class MvcHandler : IHttpAsyncHandler, IHttpHandler, IRequiresSessionState
{
// Fields
private ControllerBuilder _controllerBuilder;
private static readonly object _processRequestTag = new object();
internal static readonly string MvcVersion = GetMvcVersionString();
public static readonly string MvcVersionHeaderName = "X-AspNetMvc-Version";

// Methods
public MvcHandler(RequestContext requestContext)
{
if (requestContext == null)
{
throw new ArgumentNullException("requestContext");
}
this.RequestContext = requestContext;
}

protected internal virtual void AddVersionHeader(HttpContextBase httpContext)
{
if (!DisableMvcResponseHeader)
{
httpContext.Response.AppendHeader(MvcVersionHeaderName, MvcVersion);
}
}

protected virtual IAsyncResult BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, object state)
{
HttpContextBase base2 = new HttpContextWrapper(httpContext);
return this.BeginProcessRequest(base2, callback, state);
}

protected internal virtual IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state)
{
return SecurityUtil.ProcessInApplicationTrust<IAsyncResult>(delegate {
BeginInvokeDelegate delegate4 = null;
EndInvokeDelegate delegate5 = null;
Action action2 = null;
IController controller;
IControllerFactory factory;
this.ProcessRequestInit(httpContext, out controller, out factory);
IAsyncController asyncController = controller as IAsyncController;
if (asyncController != null)
{
if (delegate4 == null)
{
delegate4 = delegate (AsyncCallback asyncCallback, object asyncState) {
IAsyncResult result;
try
{
result = asyncController.BeginExecute(this.RequestContext, asyncCallback, asyncState);
}
catch
{
factory.ReleaseController(asyncController);
throw;
}
return result;
};
}
BeginInvokeDelegate beginDelegate = delegate4;
if (delegate5 == null)
{
delegate5 = delegate (IAsyncResult asyncResult) {
try
{
asyncController.EndExecute(asyncResult);
}
finally
{
factory.ReleaseController(asyncController);
}
};
}
EndInvokeDelegate endDelegate = delegate5;
SynchronizationContext syncContext = SynchronizationContextUtil.GetSynchronizationContext();
return AsyncResultWrapper.Begin(AsyncUtil.WrapCallbackForSynchronizedExecution(callback, syncContext), state, beginDelegate, endDelegate, _processRequestTag);
}
if (action2 == null)
{
action2 = delegate {
try
{
controller.Execute(this.RequestContext);
}
finally
{
factory.ReleaseController(controller);
}
};
}
Action action = action2;
return AsyncResultWrapper.BeginSynchronous(callback, state, action, _processRequestTag);
});
}

protected internal virtual void EndProcessRequest(IAsyncResult asyncResult)
{
SecurityUtil.ProcessInApplicationTrust(() => AsyncResultWrapper.End(asyncResult, _processRequestTag));
}

private static string GetMvcVersionString()
{
return new AssemblyName(typeof(MvcHandler).Assembly.FullName).Version.ToString(2);
}

protected virtual void ProcessRequest(HttpContext httpContext)
{
HttpContextBase base2 = new HttpContextWrapper(httpContext);
this.ProcessRequest(base2);
}

protected internal virtual void ProcessRequest(HttpContextBase httpContext)
{
SecurityUtil.ProcessInApplicationTrust(delegate {
IController controller;
IControllerFactory factory;
this.ProcessRequestInit(httpContext, out controller, out factory);
try
{
controller.Execute(this.RequestContext);
}
finally
{
factory.ReleaseController(controller);
}
});
}

private void ProcessRequestInit(HttpContextBase httpContext, out IController controller, out IControllerFactory factory)
{
if (ValidationUtility.IsValidationEnabled(HttpContext.Current) == true)
{
ValidationUtility.EnableDynamicValidation(HttpContext.Current);
}
this.AddVersionHeader(httpContext);
this.RemoveOptionalRoutingParameters();
string requiredString = this.RequestContext.RouteData.GetRequiredString("controller");
factory = this.ControllerBuilder.GetControllerFactory();
controller = factory.CreateController(this.RequestContext, requiredString);
if (controller == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, MvcResources.ControllerBuilder_FactoryReturnedNull, new object[] { factory.GetType(), requiredString }));
}
}

private void RemoveOptionalRoutingParameters()
{
RouteValueDictionary values = this.RequestContext.RouteData.Values;
foreach (string str in (from entry in values
where entry.Value == UrlParameter.Optional
select entry.Key).ToArray<string>())
{
values.Remove(str);
}
}

IAsyncResult IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
return this.BeginProcessRequest(context, cb, extraData);
}

void IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
{
this.EndProcessRequest(result);
}

void IHttpHandler.ProcessRequest(HttpContext httpContext)
{
this.ProcessRequest(httpContext);
}

// Properties
internal ControllerBuilder ControllerBuilder
{
get
{
if (this._controllerBuilder == null)
{
this._controllerBuilder = ControllerBuilder.Current;
}
return this._controllerBuilder;
}
set
{
this._controllerBuilder = value;
}
}

public static bool DisableMvcResponseHeader
{
[CompilerGenerated]
get
{
return <DisableMvcResponseHeader>k__BackingField;
}
[CompilerGenerated]
set
{
<DisableMvcResponseHeader>k__BackingField = value;
}
}

protected virtual bool IsReusable
{
get
{
return false;
}
}

public RequestContext RequestContext { get; private set; }

bool IHttpHandler.IsReusable
{
get
{
return this.IsReusable;
}
}
}

 

public abstract class ControllerBase : IController
{
    // Fields
    private DynamicViewDataDictionary _dynamicViewDataDictionary;
    private readonly SingleEntryGate _executeWasCalledGate = new SingleEntryGate();
    private TempDataDictionary _tempDataDictionary;
    private bool _validateRequest = true;
    private IValueProvider _valueProvider;
    private ViewDataDictionary _viewDataDictionary;

    // Methods
    protected ControllerBase()
    {
    }

    protected virtual void Execute(RequestContext requestContext)
    {
        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 abstract void ExecuteCore();
    protected virtual void Initialize(RequestContext requestContext)
    {
        this.ControllerContext = new ControllerContext(requestContext, this);
    }

    void IController.Execute(RequestContext requestContext)
    {
        this.Execute(requestContext);
    }

    internal void VerifyExecuteCalledOnce()
    {
        if (!this._executeWasCalledGate.TryEnter())
        {
            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, MvcResources.ControllerBase_CannotHandleMultipleRequests, new object[] { base.GetType() }));
        }
    }

    // Properties
    public ControllerContext ControllerContext { get; set; }

    public TempDataDictionary TempData
    {
        get
        {
            if ((this.ControllerContext != null) && this.ControllerContext.IsChildAction)
            {
                return this.ControllerContext.ParentActionViewContext.TempData;
            }
            if (this._tempDataDictionary == null)
            {
                this._tempDataDictionary = new TempDataDictionary();
            }
            return this._tempDataDictionary;
        }
        set
        {
            this._tempDataDictionary = value;
        }
    }

    public bool ValidateRequest
    {
        get
        {
            return this._validateRequest;
        }
        set
        {
            this._validateRequest = value;
        }
    }

    public IValueProvider ValueProvider
    {
        get
        {
            if (this._valueProvider == null)
            {
                this._valueProvider = ValueProviderFactories.Factories.GetValueProvider(this.ControllerContext);
            }
            return this._valueProvider;
        }
        set
        {
            this._valueProvider = value;
        }
    }

    [Dynamic]
    public object ViewBag
    {
        [return: Dynamic]
        get
        {
            Func<ViewDataDictionary> viewDataThunk = null;
            if (this._dynamicViewDataDictionary == null)
            {
                if (viewDataThunk == null)
                {
                    viewDataThunk = () => this.ViewData;
                }
                this._dynamicViewDataDictionary = new DynamicViewDataDictionary(viewDataThunk);
            }
            return this._dynamicViewDataDictionary;
        }
    }

    public ViewDataDictionary ViewData
    {
        get
        {
            if (this._viewDataDictionary == null)
            {
                this._viewDataDictionary = new ViewDataDictionary();
            }
            return this._viewDataDictionary;
        }
        set
        {
            this._viewDataDictionary = value;
        }
    }
}

 

public abstract class Controller : ControllerBase, IActionFilter, IAuthorizationFilter, IDisposable, IExceptionFilter, IResultFilter
{
    // Fields
    private IActionInvoker _actionInvoker;
    private ModelBinderDictionary _binders;
    private RouteCollection _routeCollection;
    private ITempDataProvider _tempDataProvider;

    // Methods
    protected Controller()
    {
    }

    protected internal ContentResult Content(string content)
    {
        return this.Content(content, null);
    }

    protected internal ContentResult Content(string content, string contentType)
    {
        return this.Content(content, contentType, null);
    }

    protected internal virtual ContentResult Content(string content, string contentType, Encoding contentEncoding)
    {
        return new ContentResult { Content = content, ContentType = contentType, ContentEncoding = contentEncoding };
    }

    protected virtual IActionInvoker CreateActionInvoker()
    {
        return new ControllerActionInvoker();
    }

    protected virtual ITempDataProvider CreateTempDataProvider()
    {
        return new SessionStateTempDataProvider();
    }

    public void Dispose()
    {
        this.Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
    }

    protected override void ExecuteCore()
    {
        this.PossiblyLoadTempData();
        try
        {
            string requiredString = this.RouteData.GetRequiredString("action");
            if (!this.ActionInvoker.InvokeAction(base.ControllerContext, requiredString))
            {
                this.HandleUnknownAction(requiredString);
            }
        }
        finally
        {
            this.PossiblySaveTempData();
        }
    }

    protected internal FileStreamResult File(Stream fileStream, string contentType)
    {
        return this.File(fileStream, contentType, null);
    }

    protected internal FileContentResult File(byte[] fileContents, string contentType)
    {
        return this.File(fileContents, contentType, null);
    }

    protected internal FilePathResult File(string fileName, string contentType)
    {
        return this.File(fileName, contentType, null);
    }

    protected internal virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName)
    {
        return new FileContentResult(fileContents, contentType) { FileDownloadName = fileDownloadName };
    }

    protected internal virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName)
    {
        return new FileStreamResult(fileStream, contentType) { FileDownloadName = fileDownloadName };
    }

    protected internal virtual FilePathResult File(string fileName, string contentType, string fileDownloadName)
    {
        return new FilePathResult(fileName, contentType) { FileDownloadName = fileDownloadName };
    }

    protected virtual void HandleUnknownAction(string actionName)
    {
        throw new HttpException(0x194, string.Format(CultureInfo.CurrentCulture, MvcResources.Controller_UnknownAction, new object[] { actionName, base.GetType().FullName }));
    }

    protected internal HttpNotFoundResult HttpNotFound()
    {
        return this.HttpNotFound(null);
    }

    protected internal virtual HttpNotFoundResult HttpNotFound(string statusDescription)
    {
        return new HttpNotFoundResult(statusDescription);
    }

    protected override void Initialize(RequestContext requestContext)
    {
        base.Initialize(requestContext);
        this.Url = new UrlHelper(requestContext);
    }

    protected internal virtual JavaScriptResult JavaScript(string script)
    {
        return new JavaScriptResult { Script = script };
    }

    protected internal JsonResult Json(object data)
    {
        return this.Json(data, null, null, JsonRequestBehavior.DenyGet);
    }

    protected internal JsonResult Json(object data, string contentType)
    {
        return this.Json(data, contentType, null, JsonRequestBehavior.DenyGet);
    }

    protected internal JsonResult Json(object data, JsonRequestBehavior behavior)
    {
        return this.Json(data, null, null, behavior);
    }

    protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding)
    {
        return this.Json(data, contentType, contentEncoding, JsonRequestBehavior.DenyGet);
    }

    protected internal JsonResult Json(object data, string contentType, JsonRequestBehavior behavior)
    {
        return this.Json(data, contentType, null, behavior);
    }

    protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
    {
        return new JsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior };
    }

    protected virtual void OnActionExecuted(ActionExecutedContext filterContext)
    {
    }

    protected virtual void OnActionExecuting(ActionExecutingContext filterContext)
    {
    }

    protected virtual void OnAuthorization(AuthorizationContext filterContext)
    {
    }

    protected virtual void OnException(ExceptionContext filterContext)
    {
    }

    protected virtual void OnResultExecuted(ResultExecutedContext filterContext)
    {
    }

    protected virtual void OnResultExecuting(ResultExecutingContext filterContext)
    {
    }

    protected internal PartialViewResult PartialView()
    {
        return this.PartialView(null, null);
    }

    protected internal PartialViewResult PartialView(object model)
    {
        return this.PartialView(null, model);
    }

    protected internal PartialViewResult PartialView(string viewName)
    {
        return this.PartialView(viewName, null);
    }

    protected internal virtual PartialViewResult PartialView(string viewName, object model)
    {
        if (model != null)
        {
            base.ViewData.Model = model;
        }
        return new PartialViewResult { ViewName = viewName, ViewData = base.ViewData, TempData = base.TempData };
    }

    internal void PossiblyLoadTempData()
    {
        if (!base.ControllerContext.IsChildAction)
        {
            base.TempData.Load(base.ControllerContext, this.TempDataProvider);
        }
    }

    internal void PossiblySaveTempData()
    {
        if (!base.ControllerContext.IsChildAction)
        {
            base.TempData.Save(base.ControllerContext, this.TempDataProvider);
        }
    }

    protected internal virtual RedirectResult Redirect(string url)
    {
        if (string.IsNullOrEmpty(url))
        {
            throw new ArgumentException(MvcResources.Common_NullOrEmpty, "url");
        }
        return new RedirectResult(url);
    }

    protected internal virtual RedirectResult RedirectPermanent(string url)
    {
        if (string.IsNullOrEmpty(url))
        {
            throw new ArgumentException(MvcResources.Common_NullOrEmpty, "url");
        }
        return new RedirectResult(url, true);
    }

    protected internal RedirectToRouteResult RedirectToAction(string actionName)
    {
        return this.RedirectToAction(actionName, (RouteValueDictionary) null);
    }

    protected internal RedirectToRouteResult RedirectToAction(string actionName, object routeValues)
    {
        return this.RedirectToAction(actionName, new RouteValueDictionary(routeValues));
    }

    protected internal RedirectToRouteResult RedirectToAction(string actionName, string controllerName)
    {
        return this.RedirectToAction(actionName, controllerName, (RouteValueDictionary) null);
    }

    protected internal RedirectToRouteResult RedirectToAction(string actionName, RouteValueDictionary routeValues)
    {
        return this.RedirectToAction(actionName, null, routeValues);
    }

    protected internal RedirectToRouteResult RedirectToAction(string actionName, string controllerName, object routeValues)
    {
        return this.RedirectToAction(actionName, controllerName, new RouteValueDictionary(routeValues));
    }

    protected internal virtual RedirectToRouteResult RedirectToAction(string actionName, string controllerName, RouteValueDictionary routeValues)
    {
        RouteValueDictionary dictionary;
        if (this.RouteData == null)
        {
            bool includeImplicitMvcValues = true;
            dictionary = RouteValuesHelpers.MergeRouteValues(actionName, controllerName, null, routeValues, includeImplicitMvcValues);
        }
        else
        {
            bool flag2 = true;
            dictionary = RouteValuesHelpers.MergeRouteValues(actionName, controllerName, this.RouteData.Values, routeValues, flag2);
        }
        return new RedirectToRouteResult(dictionary);
    }

    protected internal RedirectToRouteResult RedirectToActionPermanent(string actionName)
    {
        return this.RedirectToActionPermanent(actionName, (RouteValueDictionary) null);
    }

    protected internal RedirectToRouteResult RedirectToActionPermanent(string actionName, object routeValues)
    {
        return this.RedirectToActionPermanent(actionName, new RouteValueDictionary(routeValues));
    }

    protected internal RedirectToRouteResult RedirectToActionPermanent(string actionName, string controllerName)
    {
        return this.RedirectToActionPermanent(actionName, controllerName, (RouteValueDictionary) null);
    }

    protected internal RedirectToRouteResult RedirectToActionPermanent(string actionName, RouteValueDictionary routeValues)
    {
        return this.RedirectToActionPermanent(actionName, null, routeValues);
    }

    protected internal RedirectToRouteResult RedirectToActionPermanent(string actionName, string controllerName, object routeValues)
    {
        return this.RedirectToActionPermanent(actionName, controllerName, new RouteValueDictionary(routeValues));
    }

    protected internal virtual RedirectToRouteResult RedirectToActionPermanent(string actionName, string controllerName, RouteValueDictionary routeValues)
    {
        RouteValueDictionary implicitRouteValues = (this.RouteData != null) ? this.RouteData.Values : null;
        bool includeImplicitMvcValues = true;
        RouteValueDictionary dictionary2 = RouteValuesHelpers.MergeRouteValues(actionName, controllerName, implicitRouteValues, routeValues, includeImplicitMvcValues);
        return new RedirectToRouteResult(null, dictionary2, true);
    }

    protected internal RedirectToRouteResult RedirectToRoute(object routeValues)
    {
        return this.RedirectToRoute(new RouteValueDictionary(routeValues));
    }

    protected internal RedirectToRouteResult RedirectToRoute(string routeName)
    {
        return this.RedirectToRoute(routeName, (RouteValueDictionary) null);
    }

    protected internal RedirectToRouteResult RedirectToRoute(RouteValueDictionary routeValues)
    {
        return this.RedirectToRoute(null, routeValues);
    }

    protected internal RedirectToRouteResult RedirectToRoute(string routeName, object routeValues)
    {
        return this.RedirectToRoute(routeName, new RouteValueDictionary(routeValues));
    }

    protected internal virtual RedirectToRouteResult RedirectToRoute(string routeName, RouteValueDictionary routeValues)
    {
        return new RedirectToRouteResult(routeName, RouteValuesHelpers.GetRouteValues(routeValues));
    }

    protected internal RedirectToRouteResult RedirectToRoutePermanent(object routeValues)
    {
        return this.RedirectToRoutePermanent(new RouteValueDictionary(routeValues));
    }

    protected internal RedirectToRouteResult RedirectToRoutePermanent(string routeName)
    {
        return this.RedirectToRoutePermanent(routeName, (RouteValueDictionary) null);
    }

    protected internal RedirectToRouteResult RedirectToRoutePermanent(RouteValueDictionary routeValues)
    {
        return this.RedirectToRoutePermanent(null, routeValues);
    }

    protected internal RedirectToRouteResult RedirectToRoutePermanent(string routeName, object routeValues)
    {
        return this.RedirectToRoutePermanent(routeName, new RouteValueDictionary(routeValues));
    }

    protected internal virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName, RouteValueDictionary routeValues)
    {
        return new RedirectToRouteResult(routeName, RouteValuesHelpers.GetRouteValues(routeValues), true);
    }

    void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
    {
        this.OnActionExecuted(filterContext);
    }

    void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
    {
        this.OnActionExecuting(filterContext);
    }

    void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext)
    {
        this.OnAuthorization(filterContext);
    }

    void IExceptionFilter.OnException(ExceptionContext filterContext)
    {
        this.OnException(filterContext);
    }

    void IResultFilter.OnResultExecuted(ResultExecutedContext filterContext)
    {
        this.OnResultExecuted(filterContext);
    }

    void IResultFilter.OnResultExecuting(ResultExecutingContext filterContext)
    {
        this.OnResultExecuting(filterContext);
    }

    protected internal bool TryUpdateModel<TModel>(TModel model) where TModel: class
    {
        return this.TryUpdateModel<TModel>(model, null, null, null, base.ValueProvider);
    }

    protected internal bool TryUpdateModel<TModel>(TModel model, IValueProvider valueProvider) where TModel: class
    {
        return this.TryUpdateModel<TModel>(model, null, null, null, valueProvider);
    }

    protected internal bool TryUpdateModel<TModel>(TModel model, string[] includeProperties) where TModel: class
    {
        return this.TryUpdateModel<TModel>(model, null, includeProperties, null, base.ValueProvider);
    }

    protected internal bool TryUpdateModel<TModel>(TModel model, string prefix) where TModel: class
    {
        return this.TryUpdateModel<TModel>(model, prefix, null, null, base.ValueProvider);
    }

    protected internal bool TryUpdateModel<TModel>(TModel model, string[] includeProperties, IValueProvider valueProvider) where TModel: class
    {
        return this.TryUpdateModel<TModel>(model, null, includeProperties, null, valueProvider);
    }

    protected internal bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties) where TModel: class
    {
        return this.TryUpdateModel<TModel>(model, prefix, includeProperties, null, base.ValueProvider);
    }

    protected internal bool TryUpdateModel<TModel>(TModel model, string prefix, IValueProvider valueProvider) where TModel: class
    {
        return this.TryUpdateModel<TModel>(model, prefix, null, null, valueProvider);
    }

    protected internal bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, IValueProvider valueProvider) where TModel: class
    {
        return this.TryUpdateModel<TModel>(model, prefix, includeProperties, null, valueProvider);
    }

    protected internal bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) where TModel: class
    {
        return this.TryUpdateModel<TModel>(model, prefix, includeProperties, excludeProperties, base.ValueProvider);
    }

    protected internal bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties, IValueProvider valueProvider) where TModel: class
    {
        if (model == null)
        {
            throw new ArgumentNullException("model");
        }
        if (valueProvider == null)
        {
            throw new ArgumentNullException("valueProvider");
        }
        Predicate<string> predicate = propertyName => BindAttribute.IsPropertyAllowed(propertyName, includeProperties, excludeProperties);
        IModelBinder binder = this.Binders.GetBinder(typeof(TModel));
        ModelBindingContext context2 = new ModelBindingContext {
            ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(TModel)),
            ModelName = prefix,
            ModelState = this.ModelState,
            PropertyFilter = predicate,
            ValueProvider = valueProvider
        };
        ModelBindingContext bindingContext = context2;
        binder.BindModel(base.ControllerContext, bindingContext);
        return this.ModelState.IsValid;
    }

    protected internal bool TryValidateModel(object model)
    {
        return this.TryValidateModel(model, null);
    }

    protected internal bool TryValidateModel(object model, string prefix)
    {
        if (model == null)
        {
            throw new ArgumentNullException("model");
        }
        foreach (ModelValidationResult result in ModelValidator.GetModelValidator(ModelMetadataProviders.Current.GetMetadataForType(() => model, model.GetType()), base.ControllerContext).Validate(null))
        {
            this.ModelState.AddModelError(DefaultModelBinder.CreateSubPropertyName(prefix, result.MemberName), result.Message);
        }
        return this.ModelState.IsValid;
    }

    protected internal void UpdateModel<TModel>(TModel model) where TModel: class
    {
        this.UpdateModel<TModel>(model, null, null, null, base.ValueProvider);
    }

    protected internal void UpdateModel<TModel>(TModel model, string prefix) where TModel: class
    {
        this.UpdateModel<TModel>(model, prefix, null, null, base.ValueProvider);
    }

    protected internal void UpdateModel<TModel>(TModel model, IValueProvider valueProvider) where TModel: class
    {
        this.UpdateModel<TModel>(model, null, null, null, valueProvider);
    }

    protected internal void UpdateModel<TModel>(TModel model, string[] includeProperties) where TModel: class
    {
        this.UpdateModel<TModel>(model, null, includeProperties, null, base.ValueProvider);
    }

    protected internal void UpdateModel<TModel>(TModel model, string prefix, string[] includeProperties) where TModel: class
    {
        this.UpdateModel<TModel>(model, prefix, includeProperties, null, base.ValueProvider);
    }

    protected internal void UpdateModel<TModel>(TModel model, string prefix, IValueProvider valueProvider) where TModel: class
    {
        this.UpdateModel<TModel>(model, prefix, null, null, valueProvider);
    }

    protected internal void UpdateModel<TModel>(TModel model, string[] includeProperties, IValueProvider valueProvider) where TModel: class
    {
        this.UpdateModel<TModel>(model, null, includeProperties, null, valueProvider);
    }

    protected internal void UpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) where TModel: class
    {
        this.UpdateModel<TModel>(model, prefix, includeProperties, excludeProperties, base.ValueProvider);
    }

    protected internal void UpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, IValueProvider valueProvider) where TModel: class
    {
        this.UpdateModel<TModel>(model, prefix, includeProperties, null, valueProvider);
    }

    protected internal void UpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties, IValueProvider valueProvider) where TModel: class
    {
        if (!this.TryUpdateModel<TModel>(model, prefix, includeProperties, excludeProperties, valueProvider))
        {
            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, MvcResources.Controller_UpdateModel_UpdateUnsuccessful, new object[] { typeof(TModel).FullName }));
        }
    }

    protected internal void ValidateModel(object model)
    {
        this.ValidateModel(model, null);
    }

    protected internal void ValidateModel(object model, string prefix)
    {
        if (!this.TryValidateModel(model, prefix))
        {
            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, MvcResources.Controller_Validate_ValidationFailed, new object[] { model.GetType().FullName }));
        }
    }

    protected internal ViewResult View()
    {
        return this.View(null, null, null);
    }

    protected internal ViewResult View(object model)
    {
        return this.View(null, null, model);
    }

    protected internal ViewResult View(string viewName)
    {
        return this.View(viewName, null, null);
    }

    protected internal ViewResult View(IView view)
    {
        return this.View(view, null);
    }

    protected internal ViewResult View(string viewName, object model)
    {
        return this.View(viewName, null, model);
    }

    protected internal ViewResult View(string viewName, string masterName)
    {
        return this.View(viewName, masterName, null);
    }

    protected internal virtual ViewResult View(IView view, object model)
    {
        if (model != null)
        {
            base.ViewData.Model = model;
        }
        return new ViewResult { View = view, ViewData = base.ViewData, TempData = base.TempData };
    }

    protected internal virtual ViewResult View(string viewName, string masterName, object model)
    {
        if (model != null)
        {
            base.ViewData.Model = model;
        }
        return new ViewResult { ViewName = viewName, MasterName = masterName, ViewData = base.ViewData, TempData = base.TempData };
    }

    // Properties
    public IActionInvoker ActionInvoker
    {
        get
        {
            if (this._actionInvoker == null)
            {
                this._actionInvoker = this.CreateActionInvoker();
            }
            return this._actionInvoker;
        }
        set
        {
            this._actionInvoker = value;
        }
    }

    protected internal ModelBinderDictionary Binders
    {
        get
        {
            if (this._binders == null)
            {
                this._binders = ModelBinders.Binders;
            }
            return this._binders;
        }
        set
        {
            this._binders = value;
        }
    }

    public HttpContextBase HttpContext
    {
        get
        {
            if (base.ControllerContext != null)
            {
                return base.ControllerContext.HttpContext;
            }
            return null;
        }
    }

    public ModelStateDictionary ModelState
    {
        get
        {
            return base.ViewData.ModelState;
        }
    }

    public HttpRequestBase Request
    {
        get
        {
            if (this.HttpContext != null)
            {
                return this.HttpContext.Request;
            }
            return null;
        }
    }

    public HttpResponseBase Response
    {
        get
        {
            if (this.HttpContext != null)
            {
                return this.HttpContext.Response;
            }
            return null;
        }
    }

    internal RouteCollection RouteCollection
    {
        get
        {
            if (this._routeCollection == null)
            {
                this._routeCollection = RouteTable.Routes;
            }
            return this._routeCollection;
        }
        set
        {
            this._routeCollection = value;
        }
    }

    public RouteData RouteData
    {
        get
        {
            if (base.ControllerContext != null)
            {
                return base.ControllerContext.RouteData;
            }
            return null;
        }
    }

    public HttpServerUtilityBase Server
    {
        get
        {
            if (this.HttpContext != null)
            {
                return this.HttpContext.Server;
            }
            return null;
        }
    }

    public HttpSessionStateBase Session
    {
        get
        {
            if (this.HttpContext != null)
            {
                return this.HttpContext.Session;
            }
            return null;
        }
    }

    public ITempDataProvider TempDataProvider
    {
        get
        {
            if (this._tempDataProvider == null)
            {
                this._tempDataProvider = this.CreateTempDataProvider();
            }
            return this._tempDataProvider;
        }
        set
        {
            this._tempDataProvider = value;
        }
    }

    public UrlHelper Url { get; set; }

    public IPrincipal User
    {
        get
        {
            if (this.HttpContext != null)
            {
                return this.HttpContext.User;
            }
            return null;
        }
    }
}



posted on 2012-04-08 22:25  啊熊  阅读(489)  评论(1编辑  收藏  举报