/// <summary> /// Responds the specified context. /// </summary> /// <param name="context">The context.</param> public override void Respond(ResponderContext context) { Invariant.IsNotNull(context, "context"); ControllerContext controllerContext = context.ControllerContext; string action = controllerContext.RouteData.ActionName().ToLower(CultureInfo.CurrentCulture); ModelStateDictionary modelState = controllerContext.Controller.ViewData.ModelState; HttpResponseBase httpResponse = controllerContext.HttpContext.Response; if (KnownActionNames.Destroy.Equals(action)) { BuildResponse(null, (int)HttpStatusCode.OK, httpResponse); return; } if (!modelState.IsValid && KnownActionNames.CreateAndUpdate().Contains(action)) { IEnumerable <ModelStateError> errors = modelState.Select(ms => new { key = ms.Key, errors = ms.Value.Errors.Select(error => (error.Exception == null) ? error.ErrorMessage : error.Exception.Message) }) .Where(ms => ms.errors.Any()) // No need to include model state that does not have error .Select(e => new ModelStateError { Key = e.key, Messages = e.errors.ToList() }) .ToList(); BuildResponse(errors, 422, httpResponse); // No equivalent .NET HttpStatusCode, Unprocessable Entity, ref http://www.iana.org/assignments/http-status-codes return; } BuildResponse(context.Model, (int)(KnownActionNames.Create.Equals(action) ? HttpStatusCode.Created : HttpStatusCode.OK), controllerContext.HttpContext.Response); }
/// <summary> /// Responds the specified context. /// </summary> /// <param name="context">The context.</param> public override void Respond(ResponderContext context) { Invariant.IsNotNull(context, "context"); if (RespondToExplicitView(context)) { return; } if (RespondToExplicitRedirect(context)) { return; } // Nothing explicit so continue with default behavior ControllerContext controllerContext = context.ControllerContext; ControllerBase controller = controllerContext.Controller; string action = controllerContext.RouteData.ActionName().ToLower(CultureInfo.CurrentCulture); if (KnownActionNames.Destroy.Equals(action) || (controller.ViewData.ModelState.IsValid && KnownActionNames.CreateAndUpdate().Contains(action))) { InjectFlashMessages(context); new RedirectToRouteResult(new RouteValueDictionary(new { action = KnownActionNames.Index })).ExecuteResult(controllerContext); return; } controller.ViewData.Model = context.Model; string view = action; // If we reached here for create or update action, then the model state is not valid // so we have to toggle the view name if (KnownActionNames.Create.Equals(action)) { view = KnownActionNames.New; } else if (KnownActionNames.Update.Equals(action)) { view = KnownActionNames.Edit; } else { InjectFlashMessages(context); } (new ViewResult { ViewName = view, ViewData = controller.ViewData, TempData = controller.TempData }).ExecuteResult(controllerContext); }
/// <summary> /// Called when a request matches this controller, but no method with the specified action name is found in the controller. /// </summary> /// <param name="actionName">The name of the attempted action.</param> protected override void HandleUnknownAction(string actionName) { if (!string.IsNullOrWhiteSpace(actionName)) { // We have to set the correct HttpStatus code if the // unknown action is one of our golden 7 actions. if (KnownActionNames.All().Contains(actionName, StringComparer.OrdinalIgnoreCase)) { // if we know the action, the obvious reason it does not match due to // the incorrect http method, so lets set the correct http status code // to notify the client. HttpContext.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed; return; } } base.HandleUnknownAction(actionName); }