Reputation: 1924
I need log all GET request url + page.Title. Page title set in _Layout.cshtml as
<title>@ViewBag.Title - @S.AppName</title>
ViewBag.Title set in View.cshtml
ViewBag.Title = S.MemberDayReports;
I try use custom ActionFilterAttribute. In OnResultExecuted(..) ViewBag.Title is null.
I try override in controller OnResultExecuted but ViewBag.Title is null.
How intercept view output and log ViewBag.Title?
Upvotes: 1
Views: 1457
Reputation: 3347
I haven't checked if values set in the controller are readable this way, but you can set the viewbag like
public class FilterNameHere : IActionFilter
{
public void OnActionExecuted(ActionExecutedContext filterContext)
{
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewBag.propertyNameHere = "value here";
}
}
Then register the filter as a GlobalFilter via GlobalAsax such as
GlobalFilters.Filters.Add(new FilterNameHere());
Upvotes: 0
Reputation:
override WebViewPage
public abstract class WebViewPageAdv<T> : WebViewPage<T>
{
public string Title
{
get
{
var title = String.Format("{0} - {1}", ViewBag.Title, S.AppName);
//Because Title get in _Layout.cshtml its full page load
//and can log user GET request
return title;
}
}
}
Views\web.config
<pages pageBaseType="WebViewPageAdv">
_Layout.cshtml
<title>@this.Title</title>
Upvotes: 1
Reputation: 24901
The ViewBag
description in MSDN describes where ViewBag is used for:
The ViewBag property enables you to dynamically share values from the controller to the view. It is a dynamic object which means it has no pre-defined properties. You define the properties you want the ViewBag to have by simply adding them to the property. In the view, you retrieve those values by using same name for the property.
If you change the property of the ViewBag
in the View is not returned back to the pipeline and it is not visible in either filters or controller.
As a workaround you can set Title
property in the view - in this case it would be visible both in filters and in controller:
public ActionResult Index()
{
ViewBag.Title = "Your title";
return View();
}
Upvotes: 1
Reputation: 457
Viewbag main purpose is controller to view communication. Value are set to null after redirection.TempData keep the information for the time of an HTTP Request. This mean only from one page to another.Tye to use temp data .
or also you can update a static variable for title in each post/get. Fro this purpose you can use filter to track current and last data. ViewBag wont fullfill your requirement.
Upvotes: 1