PageRenderTime 18ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Web.Mvc;
27
28namespace CSASPNETMVCCustomActionFilter.ActionFilters
29{
30    public class MessageModifierActionFilter : ActionFilterAttribute
31    {
32        public override void OnResultExecuted(ResultExecutedContext filterContext)
33        {
34            // 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)
35
36        }
37
38        public override void OnResultExecuting(ResultExecutingContext filterContext)
39        {
40            // This event is a preprocessing event where we can customize the ViewData collection
41
42            ViewResult view = filterContext.Result as ViewResult;
43            if (view != null)
44            {
45                view.ViewData["Message"] = "[Modified by MessageModifierActionFilter.OnResultExecuting]" + view.ViewData["Message"].ToString();
46            }
47        }
48    }
49}