本文介绍了MVC和元标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在制作mvc2.
我想使用元标记.
我对meta标签和seo还是陌生的.
如何在我的页面上使用元标记?
在MVC上使用元标记的最佳方法是什么?
Hi everyone,
I am working on mvc2.
I want used Meta tags.
I am new on the meta tags and seo.
How can used meta tags on my page?
What is the best way to used meta tags on mvc?
推荐答案
Put it in your viewdata! Do something like the following...
BaseViewData.cs - this is a viewdata class that all other viewdata classes will inherit from
public class BaseViewData
{
public string Title { get; set; }
public string MetaKeywords { get; set; }
public string MetaDescription { get; set; }
}
Then your Site.Master (or whatever) class should be defined as follows:
public partial class Site : System.Web.Mvc.ViewMasterPage<BaseViewData>
{
}
Now in your Site.Master page simply have
<title><%=ViewData.Model.Title %></title>
<meta name="keywords" content="<%=ViewData.Model.MetaKeywords %>" />
<meta name="description" content="<%=ViewData.Model.MetaDescription %>" />
And you're away laughing!
HTHs, Charles
Ps. You can then expand on this idea, e.g. put a getter to your User (IPrincipal) Class into a LoggedInBaseViewData class.
或者只是您可以像这样
Or simply you can Go like this
Here is how I am currently doing it...
In the masterpage, I have a content place holder with a default title, description and keywords:
<head>
<asp:ContentPlaceHolder ID="cphHead" runat="server">
<title>Default Title</title>
<meta name="description" content="Default Description" />
<meta name="keywords" content="Default Keywords" />
</asp:ContentPlaceHolder>
</head>
And then in the page, you can override all this content:
<asp:Content ID="headContent" ContentPlaceHolderID="cphHead" runat="server">
<title>Page Specific Title</title>
<meta name="description" content="Page Specific Description" />
<meta name="keywords" content="Page Specific Keywords" />
</asp:Content>
This should give you an idea on how to set it up. Now you can put this information in your ViewData (ViewData["PageTitle"]) or include it in your model (ViewData.Model.MetaDescription - would make sense for blog posts, etc) and make it data driven.
这篇关于MVC和元标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!