2

I need to create db driven meta keywords/description. I would store these records in a database, xml format i presume; since it would be per culture.

How would i go about doing this?

any feedback, suggestions, help, greatly appreciated. thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
ShaneKm
  • 20,823
  • 43
  • 167
  • 296

4 Answers4

5

What you need to change is just the call to method that returns the keywork/description

You can use Thread.CurrentThread.CurrentUICulture to determine the user culture.

You need to change at web.config to auto change the culture.

Ex.: (Web.Config)

<globalization uiCulture="auto" culture="auto" />

(Controller)

ViewBag.Description = GetDescription(pageId, Thread.CurrentThread.CurrentUICulture.Name)

(View)

<meta name="description" content="@ViewBag.Description ">
Felipe Pessoto
  • 6,855
  • 10
  • 42
  • 73
  • +1 - used in combo with alexl's appraoch, you'd have a very nice generic approach to this – jim tollan Feb 10 '11 at 10:29
  • can you give me some info on storing these records in db?. would it be xml? or each language => each record? thanks – ShaneKm Feb 10 '11 at 13:05
  • It´s a implementation detail, but can be a Table with 5 columns: Id(PK), PageId, CultureName, Keywords and Description. PageId depends of how you manage your pages. Can be the Url or a Id of your Page if you have a CMS – Felipe Pessoto Feb 10 '11 at 15:44
4

Make a parent interface for all your model objects. and you could have:

public interface IBaseMasterViewDto
{
    int PageId { get; set; }
    string Title { get; set; }
    string MetaKeywords { get; set; }
    string MetaDescription { get; set; }
}

Thus in your master view you could use

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<IBaseMasterViewDto>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">

  <head>
    <title><%: Model.Title %></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta name="keywords" content="<%: Model.MetaKeywords %>" />
    <meta name="description" content="<%: Model.MetaDescription %>" />

Hope it helps.

alexl
  • 6,841
  • 3
  • 24
  • 29
  • +1 alex - use a very similar approach myself. the combination of this plas fujiy's answer would be the best of both. – jim tollan Feb 10 '11 at 10:28
1

1 - Get keywords/description (from your Model) in your controller
2 - Assign them to a Viewbag property 3 - Display the viewbag property in your layout (or view)

OR

Assign your model with keywords/description and give it to your view as a parameter in your controller.

About the culture :
You just have to put it as a parameter in your method controller (and in your route).
After that, you have to give this parameter to your method retrieving keywords/description.

Spilarix
  • 1,418
  • 1
  • 13
  • 24
1

Shane,

I answered a somewhat similar question here on SO a while back. I didn't cover the cultural elements, but fujiy's answer above goes towards that in a way. Also, alexl's interface is a FAR better solution to loose typed viewdata elements (as per my answer in the similar question). anyway, here's what i answered 'on the day' for that question:

MVC and Meta Tags for Search Engine Optimization

Community
  • 1
  • 1
jim tollan
  • 22,305
  • 4
  • 49
  • 63