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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12586030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:16:49  来源:igfitidea点击:

setting the page title, meta info in a view in asp.net mvc 4

asp.net-mvcasp.net-mvc-4

提问by user516883

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 中的母版页),我想设置每个视图的元和标题信息,但我看不到此选项。谢谢你的帮助。

回答by moribvndvs

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. ViewBagis a property on all razor views to a dynamic type that uses the ViewDatadictionary as its backing store. ViewDatais just a dictionary where you can store random stuff that you want to use in your view.

重要的部分是@ViewBag.Title。这种剃刀语法编码并写入ViewBag.Title. ViewBag是使用ViewData字典作为其后备存储的动态类型的所有 razor 视图的属性 。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.cshtmlin 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>

回答by HOWARD

In your method of controller.

在您的控制器方法中。

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

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

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

在你看来,有这个。 @ViewData["Title"])

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

如果标题是 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.

Razor 引擎更适合 mvc,所以我建议您在创建新项目时尝试使用 razor。希望对你有帮助。

回答by Craig Curtis

I like to set page titles dynamically using the action and controller names. You can use a library like Humanizerto 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>

回答by Nick Ryan

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

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