Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. public FooViewModel Get()
  2. {
  3. if (HttpContext.Current == null)
  4. {
  5. return new FooViewModel();
  6. }
  7.  
  8. var currentContext = new HttpContextWrapper(HttpContext.Current);
  9.  
  10. // resolve actual view model.
  11.  
  12. throw new HttpException(404, "HTTP/1.1 404 Not Found");
  13. currentContext.Response.End();
  14.  
  15. public class HomeController : Controller
  16. {
  17. public FooViewModel Foo { get; set; }
  18.  
  19. public ActionResult Index()
  20. {
  21. ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
  22.  
  23. return View();
  24. }
  25.  
  26. public class RequiresModelAttribute : ActionFilterAttribute
  27. {
  28. private readonly string _modelName;
  29.  
  30. public RequiresModelAttribute(string modelName)
  31. {
  32. _modelName = modelName;
  33. }
  34.  
  35. public override void OnActionExecuting(ActionExecutingContext filterContext)
  36. {
  37. var property = filterContext.Controller.GetType().GetProperty(_modelName);
  38. var model = property.GetValue(filterContext.Controller);
  39. if (model == null)
  40. {
  41. filterContext.Result = new HttpStatusCodeResult(404);
  42. }
  43. }
  44. }
  45.  
  46. public class HomeController : Controller
  47. {
  48. public FooViewModel Foo { get; set; }
  49.  
  50. [RequiresModel("Foo")]
  51. public ActionResult Index()
  52. {
  53. ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
  54.  
  55. return View();
  56. }
  57. }
  58.  
  59. public class HonorHttpExceptionAttribute : ActionFilterAttribute
  60. {
  61. public override void OnActionExecuting(ActionExecutingContext filterContext)
  62. {
  63. var httpException = filterContext.HttpContext.AllErrors.FirstOrDefault(x => x is HttpException) as HttpException;
  64. if (httpException != null)
  65. {
  66. filterContext.Result = new HttpStatusCodeResult(httpException.GetHttpCode());
  67. }
  68. }
  69. }
  70.  
  71. public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  72. {
  73. filters.Add(new HandleErrorAttribute());
  74. filters.Add(new HonorHttpExceptionAttribute());
  75. }
  76.  
  77. public ActionResult Index()
  78. {
  79. ViewBag.Message = "Welcome to ASP.NET MVC!";
  80. Get();
  81. return View();
  82. }
  83.  
  84. public int Get()
  85. {
  86. throw new HttpException(404, "HTTP/1.1 404 Not Found");
  87. // we don't need end the response here, we need go to result step
  88. // currentContext.Response.End();
  89.  
  90. }
  91.  
  92. protected override void OnException(ExceptionContext filterContext)
  93. {
  94. base.OnException(filterContext);
  95. if (filterContext.Exception is HttpException)
  96. {
  97. filterContext.Result = this.HttpNotFound(filterContext.Exception.Message);
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement