Posts

Showing posts from August, 2014

Umbraco Razor, How to Connect and Query to a SQL Database and Loop around the Results, and use a Helper to display the results

1. Connect to the Database (the connection string must be in the Web config file)
2. Loop around the results (this code loops around a database that stores vistits to FAQ pages, and stores the Page ID)
3. Use a helper to display the results

KEY:
Field names in Database 


@inherits umbraco.MacroEngines.DynamicNodeContext
@using WebMatrix.Data


@{
// 1 Contect to the Database and Select the items
        var DB = Database.Open("myAnalyticsConnection");
var FAQs = DB.Query("SELECT * FROM Analytics WHERE docType='FAQ'  ORDER BY numberOfClicks DESC").Take(5);

}


@{
// 2For Each of the Items(FAQs) loop arround
foreach (var FAQ in FAQs)
{
@getPageLink(FAQ.pageId)
}
}




@helper getPageLink(int thisPage)
{
//3.  A Helper to get the node URL, as this is not stored in the database
try {

var thisPageId = @Model.NodeById(@thisPage);

<li style="text-align:left;background-image: url(/images/arrow-white.png);">
<a href="@thisPageId.Url">
<span style="c…

Umbraco Razor C# Hide a site from Search engies and Visitors while it is in development

If you are working on a site that is on a live server and has the live url, you could create a page on another site with a link on it that directs to your development site.

Give your client, or whoever you want to see the development site a link to that page.  When they click the link they will be able to see the actual live website.  Without the link they are redirected to a "under development" page.



    var myReferrer = Request.UrlReferrer;

    string thisString = "" + myReferrer;

     if (thisString == null  || thisString == ("www.A-PAGE-ON-ANOTHER-SITE-WITH-A LINK.html") || Session["legit"] == "yes" )
        {

            Session["legit"] = "yes";
        }
    else
        {
           Response.Redirect("http://THE-DEVELOPMENT-SITE.com/my-page.html");
        }