views:

609

answers:

1

Here is the idea:

When the user wants to see /controller/action, then I want the page to have a "robots" meta tag with its value set to "all". When the user wants to see /controller/action?sort=hot&page=2 (i.e. it has a query string), then I want the page to have a "robots" meta tag with its value set to "noindex". Of course this is just an example and it could be another tag for the existence of this question.

At what stage in the MVC architecture could I place a hook so that the view generates the tag I want in the master page?

+2  A: 

I do something similar for generating page titles and it works well. Put your tag in your masterpage as normal:

<%= Html.Encode(ViewData["Title"]) %>

Then subclass ActionFilterAttribute and override OnActionExecuting. From there you get access to the controller context and can set your viewdata to whatever you want.

public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["title"] = "whatever";
    }

last step is to put Attribute your controllers that you want to use the filter context. You can inherit from a base controller if you want to add the attribute to all classes. There are also overloads if you want to pass parameters. In my app. I actually pass the page title.

Hope that helps.

Kyle West
Many thanks Kyle.Jutst one thing: I'm not sure about what you mean with "There are also overloads if you want to pass parameters".
Nicolas Cadilhac
If you need to pass parameters to your ActionFilter you can by providing them in the attribute. See this link: http://weblogs.asp.net/scottgu/archive/2008/07/14/asp-net-mvc-preview-4-release-part-1.aspx
Kyle West