Asp.net mvc 使用控制器操作向html头部添加元描述标记?
如何使用控制器操作将值添加到html头部的元描述标记Asp.net mvc 使用控制器操作向html头部添加元描述标记?,asp.net-mvc,meta-tags,Asp.net Mvc,Meta Tags,如何使用控制器操作将值添加到html头部的元描述标记 public ActionResult Details(int id = 0, string name = "") { Category category = db.Categories.Find(id); string seodescription = string.Empty; switch (id) { case 1: { se
public ActionResult Details(int id = 0, string name = "")
{
Category category = db.Categories.Find(id);
string seodescription = string.Empty;
switch (id)
{
case 1: { seodescription = "1"; break; }
case 2: { seodescription = "2"; break; }
case 3: { seodescription = "3"; break; }
case 4: { seodescription = "4"; break; }
case 5: { seodescription = "5"; break; }
case 8: { seodescription = "6"; break; }
default: {seodescription = string.Empty; break;}
}
if (seodescription != string.Empty)
{
// here
}
}
您可以在ViewBag中传递描述:
ViewBag.MetaDescription = "Description to use";
并在视图中渲染标记
由于您可能正在使用布局,并且meta标记位于标题中,因此您应该将代码放在布局页面中:
@if (ViewBag.MetaDescription != null) {
<meta name="description" content="@ViewBag.MetaDescription">
}
@if(ViewBag.MetaDescription!=null){
}
您可以在ViewBag中传递描述:
ViewBag.MetaDescription = "Description to use";
并在视图中渲染标记
由于您可能正在使用布局,并且meta标记位于标题中,因此您应该将代码放在布局页面中:
@if (ViewBag.MetaDescription != null) {
<meta name="description" content="@ViewBag.MetaDescription">
}
@if(ViewBag.MetaDescription!=null){
}
这里有另一种方法,仅供参考:
把这个放在你的布局文件里
@RenderSection("meta", required: false)
在你看来:
@section meta{
<meta name="description" content="Content from either the view model or just plain text">
}
@节元{
}
这种方法在某些情况下可以更好地为您服务,例如如果您希望前端人员能够编辑标签。这里有一种替代方法,仅供参考: 把这个放在你的布局文件里
@RenderSection("meta", required: false)
在你看来:
@section meta{
<meta name="description" content="Content from either the view model or just plain text">
}
@节元{
}
在某些情况下,这种方法可以更好地为您服务,例如,如果您希望前端人员能够编辑标记。您应该更清楚地知道您所谈论的是html头元描述标记。编辑问题以包含此内容。您应该更清楚地知道您所谈论的是html头元描述标记。编辑问题以包含此内容。