PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSASPNETMVCCustomActionFilter/ActionFilters/MessageModifierActionFilter.cs

#
C# | 49 lines | 22 code | 5 blank | 22 comment | 2 complexity | 10d866cef57d6968ccf770172d1dce95 MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: MessageModifierActionFilter.cs
  3. * Project: CSASPNETMVCCustomActionFilter
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * The CSASPNETMVCCustomActionFilter sample demonstrates how to use C# codes to
  7. * create custom ActionFilters for ASP.NET MVC web application. In this sample,
  8. * there are two custom ActionFilters, one is used for customizing ViewData
  9. * before page view result get executed and rendered; another is used for
  10. * perform logging within the various events during the processing of custom
  11. * ActionFilters.
  12. *
  13. *
  14. * This source is subject to the Microsoft Public License.
  15. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  16. * All other rights reserved.
  17. *
  18. * History:
  19. * * 10/10/2009 1:35 PM Steven Cheng Created
  20. ***************************************************************************/
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using System.Text;
  25. using System.Web.Mvc;
  26. namespace CSASPNETMVCCustomActionFilter.ActionFilters
  27. {
  28. public class MessageModifierActionFilter : ActionFilterAttribute
  29. {
  30. public override void OnResultExecuted(ResultExecutedContext filterContext)
  31. {
  32. // If we modify the ViewData here, changes will not be reflected in the final PageView since it is too late here( Result has already been executed)
  33. }
  34. public override void OnResultExecuting(ResultExecutingContext filterContext)
  35. {
  36. // This event is a preprocessing event where we can customize the ViewData collection
  37. ViewResult view = filterContext.Result as ViewResult;
  38. if (view != null)
  39. {
  40. view.ViewData["Message"] = "[Modified by MessageModifierActionFilter.OnResultExecuting]" + view.ViewData["Message"].ToString();
  41. }
  42. }
  43. }
  44. }