简体   繁体   中英

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. Originally the Master template page was using WebForms, However the whole project has been converted to Razor pages. So the meta tags and descriptions were pulled from Umbraco using the following:

<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. I am no expert and am fairly knew to Umbraco etc. Is there a way I can perform the same functionality using razor pages? As it stands using the above code, Visual studio will say that umbraco tag is an unrecognised namespace.

This is an MVC project if that provides any further complications/information. Thanks in advance, any help will be greatly appreciated.

Old post, but figured I'd add solution I found for MVC

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. Since you are using Umbraco 7.2.2, it uses MVC as default,

In MVC templates you can called it like this.

@Umbraco.Field("AliasOfTheField")

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

So for the meta description, and meta keywords it would look like this.

@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. That alias name is what I was checking the CurrentPage.HasProperty("") for.

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

based web app you have to use either htmlhelpers or html tags in asp.net mvc.

for ex :-

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

...or simple html tags only..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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