在asp.net mvc 4中的视图中设置页面标题,元信息

时间:2022-12-04 22:57:29

I am very new to MVC and I am updating a web form application to mvc. I have a shared layout (masterpage in webforms), I would like to set the meta and title information per view but I see no options for this. thanks for any help.

我是MVC的新手,我正在将一个Web表单应用程序更新为mvc。我有一个共享布局(webforms中的母版页),我想为每个视图设置元和标题信息,但我看不到这个选项。谢谢你的帮助。

4 个解决方案

#1


34  

Typically, in your layout, you'll have something like this:

通常,在您的布局中,您将拥有以下内容:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>@ViewBag.Title</title>
     <!-- the rest omitted for brevity -->

The important part is @ViewBag.Title. This bit of razor syntax encodes and writes the value of ViewBag.Title. ViewBag is a property on all razor views to a dynamic type that uses the ViewData dictionary as its backing store. ViewData is just a dictionary where you can store random stuff that you want to use in your view.

重要的部分是@ ViewBag.Title。这一部分剃刀语法编码并写入ViewBag.Title的值。 ViewBag是动态类型的所有剃刀视图上的属性,它使用ViewData字典作为其后备存储。 ViewData只是一个字典,您可以在其中存储要在视图中使用的随机内容。

In your controller, layout, or view, you can get or set ViewBag.Title. Here's an example of how to set it in a view that uses your layout (called _Layout.cshtml in this example):

在控制器,布局或视图中,您可以获取或设置ViewBag.Title。这是一个如何在使用您的布局的视图中设置它的示例(在此示例中称为_Layout.cshtml):

@{
   ViewBag.Title = "My View's Title";
   Layout = "~/Views/Shared/_Layout.cshtml";
}

You can access the model metadata from ViewData.ModelMetadata. In this example, I enumerate the properties of the model and display the names:

您可以从ViewData.ModelMetadata访问模型元数据。在此示例中,我枚举模型的属性并显示名称:

<ul>
@foreach (var property in ViewData.ModelMetadata.Properties)
{
  <li>@property.PropertyName</li>
}
</ul>

#2


4  

In your method of controller.

在你的控制器方法。

ViewData["Title"] = "this is page one title";

ViewData [“Title”] =“这是第一页标题”;

in you view, have this. @ViewData["Title"])

在你看来,有这个。 @ViewData [ “标题”])

if title is html, it should be @html.raw(ViewData["TopMessage"])

如果title是html,那应该是@html.raw(ViewData [“TopMessage”])

Razor engine is better for mvc, so I recommend you try razor when you create a new project. hope it help you.

剃刀引擎更适合mvc,因此我建议您在创建新项目时尝试使用剃须刀。希望它对你有所帮助。

#3


2  

I like to set page titles dynamically using the action and controller names. You can use a library like Humanizer to convert "SomeActionName" into "Some action name":

我喜欢使用动作和控制器名称动态设置页面标题。您可以使用像Humanizer这样的库将“SomeActionName”转换为“Some action name”:

public static class HtmlHelperExtensions
{
    public static MvcHtmlString GetPageTitle(this HtmlHelper helper)
    {
        var actionName = helper.GetRouteDataValue("action");
        var controllerName = helper.GetRouteDataValue("controller");

        return new MvcHtmlString(controllerName.Humanize() + " - " + actionName.Humanize());
    }

    private static string GetRouteDataValue(this HtmlHelper helper, string value)
    {
        return helper.ViewContext.RouteData.Values[value].ToString();
    }
}

and then in your _Layout:

然后在你的_Layout中:

<title>@Html.GetPageTitle()</title>

#4


0  

You need to set Viewbag.Title These articles look relevant and will give you some pointers:

你需要设置Viewbag.Title这些文章看起来很相关,并会给你一些指示:

#1


34  

Typically, in your layout, you'll have something like this:

通常,在您的布局中,您将拥有以下内容:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>@ViewBag.Title</title>
     <!-- the rest omitted for brevity -->

The important part is @ViewBag.Title. This bit of razor syntax encodes and writes the value of ViewBag.Title. ViewBag is a property on all razor views to a dynamic type that uses the ViewData dictionary as its backing store. ViewData is just a dictionary where you can store random stuff that you want to use in your view.

重要的部分是@ ViewBag.Title。这一部分剃刀语法编码并写入ViewBag.Title的值。 ViewBag是动态类型的所有剃刀视图上的属性,它使用ViewData字典作为其后备存储。 ViewData只是一个字典,您可以在其中存储要在视图中使用的随机内容。

In your controller, layout, or view, you can get or set ViewBag.Title. Here's an example of how to set it in a view that uses your layout (called _Layout.cshtml in this example):

在控制器,布局或视图中,您可以获取或设置ViewBag.Title。这是一个如何在使用您的布局的视图中设置它的示例(在此示例中称为_Layout.cshtml):

@{
   ViewBag.Title = "My View's Title";
   Layout = "~/Views/Shared/_Layout.cshtml";
}

You can access the model metadata from ViewData.ModelMetadata. In this example, I enumerate the properties of the model and display the names:

您可以从ViewData.ModelMetadata访问模型元数据。在此示例中,我枚举模型的属性并显示名称:

<ul>
@foreach (var property in ViewData.ModelMetadata.Properties)
{
  <li>@property.PropertyName</li>
}
</ul>

#2


4  

In your method of controller.

在你的控制器方法。

ViewData["Title"] = "this is page one title";

ViewData [“Title”] =“这是第一页标题”;

in you view, have this. @ViewData["Title"])

在你看来,有这个。 @ViewData [ “标题”])

if title is html, it should be @html.raw(ViewData["TopMessage"])

如果title是html,那应该是@html.raw(ViewData [“TopMessage”])

Razor engine is better for mvc, so I recommend you try razor when you create a new project. hope it help you.

剃刀引擎更适合mvc,因此我建议您在创建新项目时尝试使用剃须刀。希望它对你有所帮助。

#3


2  

I like to set page titles dynamically using the action and controller names. You can use a library like Humanizer to convert "SomeActionName" into "Some action name":

我喜欢使用动作和控制器名称动态设置页面标题。您可以使用像Humanizer这样的库将“SomeActionName”转换为“Some action name”:

public static class HtmlHelperExtensions
{
    public static MvcHtmlString GetPageTitle(this HtmlHelper helper)
    {
        var actionName = helper.GetRouteDataValue("action");
        var controllerName = helper.GetRouteDataValue("controller");

        return new MvcHtmlString(controllerName.Humanize() + " - " + actionName.Humanize());
    }

    private static string GetRouteDataValue(this HtmlHelper helper, string value)
    {
        return helper.ViewContext.RouteData.Values[value].ToString();
    }
}

and then in your _Layout:

然后在你的_Layout中:

<title>@Html.GetPageTitle()</title>

#4


0  

You need to set Viewbag.Title These articles look relevant and will give you some pointers:

你需要设置Viewbag.Title这些文章看起来很相关,并会给你一些指示: