Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jesper Skjønnemand 65 posts 440 karma points c-trib
    Dec 05, 2018 @ 11:50
    Jesper Skjønnemand
    0

    In my master template I have this code

    @if (CurrentPage.seoIndex == true) 
    {<meta name="robots" content="index">}
    else if (CurrentPage.seoIndex == false) 
    {<meta name="robots" content="noindex">}
    
    @if (CurrentPage.seoFollow == true) 
    {<meta name="robots" content="follow">}
    else if (CurrentPage.seoFollow == false) 
    {<meta name="robots" content="nofollow">}
    

    It creates this

    <meta name="robots" content="noindex">
    <meta name="robots" content="follow">
    

    which is nice, but this would be nicer

    <meta name="robots" content="noindex,nofollow">
    

    Any suggestions to achieve this?

    Best

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Dec 05, 2018 @ 20:51
    Jan Skovgaard
    100

    Hi Jesper

    One way of achieving it could be to do the following:

    @{
        string seoIndexValue = "noindex";
        string seoFollowValue = "nofollow";
    
        if(CurrentPage.seoIndex == true){
            seoIndexValue = "index";
        }
    
        if(CurrentPage.seoFollow == true){
            seoFollowValue = "follow";
        }
    }
    
    <meta name="robots" content="@seoIndexValue, @seoFollowValue" />
    

    So by setting up 2 variables for holding the desired values it becomes a bit easier. By default we will set them to noindex and nofollow and then do 2 checkes on each of the settings on the currentpage and overwrite the initial value with the value we want when a setting is true.

    In the end we reference the variables in the content attribute separated by a comma.

    I hope this works for you - There are most likely other ways of doing this but this is what I could come up with from the top of my mind and it should work just fine :)

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft