在 MVC 中为 Umbraco 中的 SEO 渲染视图 [英] Render view in MVC for SEO in Umbraco

查看:27
本文介绍了在 MVC 中为 Umbraco 中的 SEO 渲染视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 umbraco cms 中呈现 seo 标签的视图.但是,我不确定如何做到这一点.有什么资料可以查吗?

I am trying to render a view for seo tags in umbraco cms. However I am not sure how it can be done. Is there some source I can look up to?

推荐答案

SEO 和社交媒体的标签可以合并,因为它们共享一些相同的信息.

Tags for SEO and social media could be combined, as they share some of the same information.

在我的 umbraco 安装中,我向网站根节点、页面节点和成员节点添加了以下属性:

On my umbraco installation I have added the following properties to website root node, page nodes and member nodes:

顶部节点:

  • 站点名称 - 文本框
  • siteDescription - textarea
  • siteLocale - 文本框(即 nb_NO)
  • facebookPageUrl- 文本框
  • facebookAppId - 文本框
  • twitterUserName - 文本框
  • siteLogo - 媒体选择器

在页面上

  • pageTitle - 文本框
  • pageSnippet - 关于页面内容的 160 个字符的文本框
  • 作者 - 成员选择器

关于会员

  • facebookProfilePage - 文本框
  • twitterUserName - 文本框
  • memberProfileUrl - 文本框

然后我使用以下代码在所有页面上呈现头部元标记:

Then i use the following code to render head meta tags on all pages:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{


        //  SITE INFORMATION 
        string site_locale = "en_US";
        if (Model.Content.AncestorOrSelf(1).HasProperty("siteLocale") && (Model.Content.GetPropertyValue<string>("siteLocale") != ""))
        { site_locale = Model.Content.GetPropertyValue<string>("siteLocale"); };

        // Check if root node has defined site name, otherwise use domain name.
        string domain = System.Web.HttpContext.Current.Request.Url.Scheme + "://" + System.Web.HttpContext.Current.Request.Url.Authority;
        string site_name = Model.Content.AncestorOrSelf(1).HasProperty("siteName") ? Model.Content.AncestorOrSelf(1).GetPropertyValue("siteName").ToString() : HttpContext.Current.Request.Url.Host.ToString();

        // Check if site description exist, and default text if not.
        string site_description = Model.Content.AncestorOrSelf(1).HasProperty("siteDescription") ? Model.Content.AncestorOrSelf(1).GetPropertyValue("siteDescription").ToString() : "The website " + HttpContext.Current.Request.Url.Host.ToString();
        string site_logo_url = "";
        string site_logo_height = "";
        string site_logo_width = "";

        if (Model.Content.AncestorOrSelf(1).HasProperty("siteLogo") && (Model.Content.AncestorOrSelf(1).GetPropertyValue<int>("siteLogo") != 0))
        {
            var logo = Umbraco.TypedMedia(Model.Content.AncestorOrSelf(1).GetPropertyValue("siteLogo").ToString());
            site_logo_url = domain + logo.Url;
            site_logo_height = logo.GetPropertyValue<string>("umbracoHeight");
            site_logo_width = logo.GetPropertyValue<string>("umbracoWidth");
        }


        string site_fb_app_id = Model.Content.AncestorOrSelf(1).GetPropertyValue("facebookAppId").ToString();
        string site_fb_page_url = Model.Content.AncestorOrSelf(1).GetPropertyValue("facebookPageUrl").ToString();
        string site_twitter_username = Model.Content.AncestorOrSelf(1).GetPropertyValue("twitterUserName").ToString();

        // PAGE INFORMATION 
        // Use page title if defined, otherwise Model.Content.Name
        string page_url = Model.Content.Url;
        string page_title = Model.Content.HasProperty("pageTitle") ? Model.Content.GetPropertyValue("pageTitle").ToString() : Model.Content.Name.ToString();
        DateTime page_created_date = Model.Content.CreateDate;
        DateTime page_updated_date = Model.Content.UpdateDate;

        string page_description = Model.Content.HasProperty("pageSnippet") ? Model.Content.GetPropertyValue("pageSnippet").ToString() : "";

        // AUTHOR INFORMATION 
        // Check for information about the author of this page
        bool hasAuthorInfo = false;
        if (Model.Content.HasProperty("author") && (Model.Content.GetPropertyValue("author").ToString() != ""))
        {
            hasAuthorInfo = true;
        }
        string author_name = "";
        string author_facebook_page = "";
        string author_twitter_username = "";
        string author_member_profile_url = "";
        if (hasAuthorInfo)
        {
            var author = Members.GetById((int)Model.Content.GetPropertyValue("author"));
            author_name = author.Name;
            author_facebook_page = author.GetPropertyValue("facebookProfilePage").ToString();
            author_twitter_username = author.GetPropertyValue("twitterUserName").ToString();
            author_member_profile_url = author.GetPropertyValue("memberProfileUrl").ToString();
        }

        // FEATURED IMAGE
        // Check if there is a featured image for this page
        bool hasFeaturedImage = false;
        string page_featuredimage_url = "";

        if (Model.Content.HasProperty("featuredImage") && (Model.Content.GetPropertyValue<int>("featuredImage") != 0))
        {
            hasFeaturedImage = true;
        }

        if (hasFeaturedImage)
        {
            var image = Umbraco.TypedMedia(Model.Content.GetPropertyValue("featuredImage").ToString());
            page_featuredimage_url = domain + image.GetCropUrl("1200x630"); // Preferred size by Facebook
        }
    }
    <meta name="description" content="@page_description" />
    <meta property="og:title" content="@page_title" />
    <meta property="og:site_name" content="@site_name" />
    <meta property="og:url" content="@page_url" />
    <meta property="og:description" content="@page_description" />
    <meta property="og:image" content="@page_featuredimage_url" />
    <meta property="fb:app_id" content="@site_fb_app_id" />
    <meta property="og:type" content="article" />
    <meta property="og:locale" content="nb_NO" />
    <meta property="article:author" content="@author_facebook_page" />
    <meta property="article:publisher" content="@site_fb_page_url" />
    <meta name="twitter:title" content="@page_title" />
    <meta name="twitter:description" content="@page_description" />
    <meta name="twitter:image:src" content="@page_featuredimage_url" />
    <meta name="twitter:card" content="summary_large_image" />
    <meta name="twitter:site" content="@site_twitter_username" />
    <meta name="twitter:creator" content="@author_twitter_username" />

    <script type="application/ld+json">
        {
        "@@context": "http://schema.org",
        "@@type": "NewsArticle",
        "mainEntityOfPage":{
        "@@type":"WebPage",
        "@@id":"@page_url"
        },
        "headline": "@page_title",
        "image": {
        "@@type": "ImageObject",
        "url": "@page_featuredimage_url",
        "height": 630,
        "width": 1200
        },
        "datePublished": "@page_created_date.ToString("o")",
        "dateModified": "@page_updated_date.ToString("o")",
        "author": {
        "@@type": "Person",
        "name": "@author_name"
        },
        "publisher": {
        "@@type": "Organization",
        "name": "@site_name",
        "logo": {
        "@@type": "ImageObject",
        "url": "@site_logo_url",
        "width": "@site_logo_width",
        "height": "@site_logo_height"
        }
        },
        "description": "@page_description"
        }
    </script>

这篇关于在 MVC 中为 Umbraco 中的 SEO 渲染视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆