简体   繁体   English

如何根据查询字符串参数在母版页上添加元标记

[英]How to add meta tags on master page dependent on query string parameter

How do you add meta tags that change depending on the browser's query string parameter? 如何添加根据浏览器的查询字符串参数更改的元标记?

For example, on my blog site the blogs are read from a database and a sample blog would be http://www.example.com/blog/blog.aspx?ID=32 例如,在我的博客站点上,从数据库中读取了博客,示例博客为http://www.example.com/blog/blog.aspx?ID=32

I'd like the meta tags to be customized for different blog posts, which all read from the blog.aspx file. 我希望针对不同的博客文章自定义meta标签,这些文章均从blog.aspx文件中读取。

The following posts gives me an idea, but how do I implement a statement to read the query string parameter? 以下文章为我提供了一个思路,但是如何实现一条语句来读取查询字符串参数?

How to add meta tags on a master page for ASP.Net MVC 2 如何在ASP.Net MVC 2的母版页上添加元标记

How to pass page's meta tags in ASP.NET MVC? 如何在ASP.NET MVC中传递页面的元标记?

Typically in this scenario, I'd have a base class for all view models, eg: 通常,在这种情况下,我将为所有视图模型提供基类 ,例如:

public abstract BasicViewModel
{
    public List<string> MetaTags {get; set; }
}

Now, if we assume that every view model across our site inherits from this view model, then we can have a strongly typed view model in our layout. 现在,如果我们假设站点中的每个视图模型都继承自该视图模型,则我们可以在布局中使用强类型的视图模型。

@model BasicViewModel
<html>
    <head>
       <!-- Render meta tags here from view model -->
    </head>
    @RenderBody()
</html>

I've omitted the fact that a Meta tag may be more intricate than a string, but you get the idea. 我已经忽略了一个事实,即Meta标记可能比字符串更复杂,但是您明白了。

In your controllers, provided that their view models derive from this - you are free to add any meta tags you wish to the view model. 在您的控制器中,只要他们的视图模型是从此派生的-您就可以随意将任何元标记添加到视图模型。 This also goes towards page titles and anything else common to every page (Google Analytic account details, for example!). 这也涉及页面标题和每个页面的其他通用内容(例如,Google Analytic帐户详细信息!)。

To get dynamic meta keywords and description, here goes my solution using Action Filter. 为了获得动态的meta关键字和描述,这是我使用动作过滤器的解决方案。

Create a Action filter and handle its OnActionExecuted method - 创建一个动作过滤器并处理其OnActionExecuted方法-

public class MetaDataAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var key = filterContext.RouteData.Values["id"];

        // using key get proper keywords and description for page

        filterContext.Controller.ViewBag.MetaKeywords = "1, 2, 3";
        filterContext.Controller.ViewBag.MetaDescription = "This is new description";
    }
}

Then mark your controller action with the metadata attribute - 然后使用元数据属性标记您的控制器操作-

    [MetaData]
    public ActionResult Index(string id)
    {
        return View();
    }

If id is the dynamic querystring which you are expecting to the action, then at the end of action execution when OnActionExecuted is called, id will be accessed in the Action filter using RouteData. 如果id是您期望动作执行的动态查询字符串,则在动作执行结束时调用OnActionExecuted时,将使用RouteData在动作过滤器中访问id。

Say for example, of you reach the URL - http://localhost:5738/Blog/Index/123 , then id will be accessed in the Action Filter in this way - 例如,假设您到达URL- http://localhost:5738/Blog/Index/123 ,则将以这种方式在操作过滤器中访问http://localhost:5738/Blog/Index/123

在此处输入图片说明

Then based on the id value, you can set the proper keywords and description in the ViewBag. 然后,基于id值,您可以在ViewBag中设置适当的关键字和描述。 Then from the Viewbag, you can get the values in the _Layout.cshtml as shown below - 然后从Viewbag,你可以在获取值_Layout.cshtml如下图所示-

<meta name="keywords" content="@ViewBag.MetaKeywords">
<meta name="description" content="@ViewBag.MetaDescription"/>

As a result, when page is rendered your meta keywords and description will be reflected as below - 因此,在呈现页面时,您的元关键字和描述将反映如下-

<meta name="keywords" content="1, 2, 3">
<meta name="description" content="This is new description"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM