I was wondering what, if there is one, is the best practice for including SEO content such as meta descriptions and keywords in an ASP.NET MVC (I'm using v3 RC) view. My initial plan is to create an actionfilter, applied globally to actions, to pull the relevant data from a data store and pass it as viewdata to the view.
My questions are: 1) Do you foresee any problems with this approach? 2) Are there any more suitable approaches? 3) what is an appropriate data store to use here - should i pull from the DB (or cache if the data is available), use resource files, config files, etc?
Thanks in advance,
JP
Based on ASP.NET, ASP.NET MVC allows software developers to build a web application as a composition of three roles: Model, View and Controller. The MVC model defines web applications with 3 logic layers: Model (business layer) View (display layer)
The main advantages of ASP.net MVC are: Enables the full control over the rendered HTML. Provides clean separation of concerns(SoC). Enables Test Driven Development (TDD).
ASP.NET MVC lets you use features such as forms authentication and Windows authentication, URL authorization, membership and roles, output and data caching, session and profile state management, health monitoring, the configuration system, and the provider architecture.
A Model, in the context of an ASP.NET Model View Controller (MVC), is the representation of the data being posted to the Controller, the data being worked on in a View or the representation of the domain specific entities operating in the business tier. The Model contains core application information.
I would use attributes on my controller actions and add them to the ViewData
in my base controller in the method OnExecutingAction
.
The motivation to put it in the controller and not the view is that it's really more information about the actual action than about the view. And you can use it when returning different kinds of formats like json or xml.
Controller
class MyController { [MetaKeywords("hello,world,something,else")] [MetaDescription("Tells you how to greet the world")] ActionResult Hello() { return View(); } }
You could always use a resource file instead of plain strings.
in base controller:
protected override void OnActionExecuting(ActionExecutingContext filterContext) { var keywords = filterContext.ActionDescriptor.GetCustomAttributes(typeof(MetaKeywordsAttribute), false); if (keywords.Length == 1) ViewData["MetaKeywords"] = keywords.Value; var description = filterContext.ActionDescriptor.GetCustomAttributes(typeof(MetaDescriptionAttribute), false); if (description.Length == 1) ViewData["MetaDescription"] = description.Value; base.OnActionExecuting(filterContext); }
In your layout
<meta name="keywords" value="@View.MetaKeywords" />
And here is the answers your questions: =)
1) Do you foresee any problems with this approach?
Nope. It's a fine approach.
2) Are there any more suitable approaches?
Just gave you an alternative.
3) what is an appropriate data store to use here - should i pull from the DB (or cache if the data is available), use resource files, config files, etc?
I would put it in plain text (if you don't have to support multiple languages), else in a resource file. This information is typically not changed unless the view or controller is changed (where a recompilation is needed anyway). Hence no need for a more dynamic source.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With