Created
October 18, 2010 12:44
-
-
Save joshka/632146 to your computer and use it in GitHub Desktop.
Favour composition over inheritance, in response to http://trycatchfail.com/blog/post/Globally-handling-status-messages-in-ASPNET-MVC.aspx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestController : Controller | |
{ | |
public ActionResult Index() | |
{ | |
return View("Index").WithStatus("Some message"); | |
} | |
} | |
public static class StatusResultExtensions | |
{ | |
public static ActionResult WithStatus(this ActionResult result, string status) | |
{ | |
return new StatusResult(result, status); | |
} | |
} | |
public class StatusResult : ActionResult | |
{ | |
private readonly ActionResult _result; | |
private readonly string _status; | |
public StatusResult(ActionResult result, string status) | |
{ | |
_result = result; | |
_status = status; | |
} | |
public override void ExecuteResult(ControllerContext context) | |
{ | |
context.Controller.TempData["Status"] = _status; | |
_result.ExecuteResult(context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment