简体   繁体   English

Razor语法中的Umbraco:Item标签

[英]Umbraco:Item tags in Razor syntax

Using a master template, the idea was to be able to pull the meta tags and descriptions from Umbraco. 使用主模板,其想法是能够从Umbraco中提取元标记和描述。 Originally the Master template page was using WebForms, However the whole project has been converted to Razor pages. 最初,主模板页面使用的是WebForms,但是整个项目已转换为Razor页面。 So the meta tags and descriptions were pulled from Umbraco using the following: 因此,使用以下命令从Umbraco中提取了元标记和描述:

<umbraco:Item field="MetaKeywords" insertTextBefore="&lt;meta name=&quot;keywords&quot; content=&quot;" insertTextAfter="&quot; /&gt;" runat="server"></umbraco:Item>
<umbraco:Item field="MetaDescription" insertTextBefore="&lt;meta name=&quot;Description&quot; content=&quot;" insertTextAfter="&quot; /&gt;" runat="server"></umbraco:Item>

However after doing some reading (and correct me if i'm wrong) the umbraco item tag cannot be used with the razor syntax. 但是,在做完一些阅读后(如果我错了,请纠正我),umbraco item标签不能与razor语法一起使用。 I am no expert and am fairly knew to Umbraco etc. Is there a way I can perform the same functionality using razor pages? 我不是专家,并且对Umbraco等非常了解。是否可以使用剃须刀页面执行相同的功能? As it stands using the above code, Visual studio will say that umbraco tag is an unrecognised namespace. 在使用上述代码的情况下,Visual Studio会说umbraco标记是无法识别的名称空间。

This is an MVC project if that provides any further complications/information. 如果这提供了进一步的复杂性/信息,则这是一个MVC项目。 Thanks in advance, any help will be greatly appreciated. 在此先感谢您的帮助。

Old post, but figured I'd add solution I found for MVC 旧帖子,但想我会添加我为MVC找到的解决方案

https://our.umbraco.org/forum/using/ui-questions/22198-Meta-Tags https://our.umbraco.org/forum/using/ui-questions/22198-Meta-Tags


The reason why this old solution not works for you is that, it´s for websites that running in WebForms mode. 这个旧解决方案不适合您的原因是,它适用于以WebForms模式运行的网站。 Since you are using Umbraco 7.2.2, it uses MVC as default, 由于您使用的是Umbraco 7.2.2,因此默认使用MVC,

In MVC templates you can called it like this. 在MVC模板中,您可以这样称呼它。

@Umbraco.Field("AliasOfTheField")

https://our.umbraco.org/Documentation/Reference/Mvc/views#RenderingafieldwithUmbracoHelper https://our.umbraco.org/Documentation/Reference/Mvc/views#RenderingafieldwithUmbracoHelper

So for the meta description, and meta keywords it would look like this. 因此,对于meta描述和meta关键字,它看起来像这样。

@Umbraco.Field("metaDescription")
@Umbraco.Field("metaKeywords")

I have managed to get this to work now. 我已经设法使它开始工作。 For anyone else that comes across this issue in the future this is how I got it to work. 对于以后遇到此问题的任何其他人,这就是我如何使其工作。 I checked if the current page had the property I was looking form, then I assigned the keywords and descriptions based on that property. 我检查了当前页面是否具有我正在查找的属性,然后根据该属性分配了关键字和描述。

 @if (CurrentPage.HasProperty("SeoKeywords") && CurrentPage.HasProperty("SeoDescription"))
    {
        <meta name="keywords" content="@CurrentPage.SeoKeywords" />
        <meta name="description" content="@CurrentPage.SeoDescription" />
    }

On Umbraco the base page document type that I am working with, has a property for both the keywords and descriptions, each with an alias name. 在Umbraco上,我正在使用的基础文档类型具有关键字和描述的属性,每个属性都有一个别名。 That alias name is what I was checking the CurrentPage.HasProperty("") for. 该别名是我正在检查CurrentPage.HasProperty(“”)的名称。

This now works for me. 现在这对我有用。 Hope this may help provide a little bit of help to anyone else. 希望这可以为其他任何人提供一点帮助。 Also thanks to 'Exception' for clarifying the original question. 也感谢“例外”澄清了原始问题。

No Umbraco tags are not supported(till now as shown in your question) in simple razor engine 简单的剃须刀引擎中不支持Umbraco标签(直到您的问题中所示为止)

based web app you have to use either htmlhelpers or html tags in asp.net mvc. 基于Web的应用程序,您必须在asp.net mvc中使用htmlhelpers或html标签。

for ex :- 对于前:-

@Html.TextBoxFor(),@Html.TextArea()

...or simple html tags only.. ...或仅使用简单的html标签。

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

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