Showing posts with label razor. Show all posts
Showing posts with label razor. Show all posts

8/21/12

Deploy Website from WebMatrix to Azure

og-bit.com
If you have created a web application on WebMatrix and want to host this application on a Windows Azure website, you will need to take a few additional steps to make sure your web application gets deployed to the correct location of your website.

After creating the website on Windows Azure, you will get a page indicating that the site has been created.




You can now click on the Download Publishing Profile link. This downloads a XML file which you should save. This file contains the publishing settings that are needed to be able to deploy the files to the correct location. The file looks a follows:



On the XML file, you can see the server settings as well as credentials to let you deploy the files to the correct Azure website. Some of the text you see on the file will match your application settings.

You now need to open your WebMatrix project and import this file.  On WebMatrix, click on the Remote tab. Now click on the Settings toolbar button.



This opens the project publish settings. The next step is to click on the Import publish settings link and select the file you just downloaded.

The previous step should populate all the settings. You can now click the Validate Connection button and you should get a message indicating that the connection was successful as shown below:



We are now ready to deploy the website.  On WebMatrix, click the Publish toolbar button.  You should see a Publish Preview screen showing all the files that are about to be deployed. On the first deployment, all the files will be deployed, so you should be patient and let the publishing process complete. If you do not have too many files, this will probably be a few seconds.  For additional deployment, only changes will be published.   WebMatrix should display a progress status bar at the bottom which looks similar to this:





When it is done, the status should read Complete, and the website URL should be displayed. Click on the URL to view your new website running on Azure.

I hope this instructions help your deployment be an easy experience.

8/19/12

Functions in ASP.Net Razor Web Pages


When using Razor Web Pages, it is very common to embed dynamic content on the HTML markup by using a server variable with the following format:

@{
   var message = "Hello";
}

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <h1>@message</h1>
    </body>
</html>

This renders a page with a simple hello message. In some instances, we may want the message to be more dynamic and to display a message based on some condition. For those cases, we can leverage the @functions block feature of the Razor syntax. The purpose of this block is to let us add functions that can be called from within the page.  Since each page is defined as a class, we can add functions and properties to support more complex functionality on Web Pages.  Let’s look at this example:

@functions {
    string GetMessage(){
      var req = Request["typ"];
      var message = "Hello";
      if (req=="sales"){
          message ="Welcome to sales";
      }
      else if (req=="support"){         
          message = "Welcome to Support";
      }
      return message;
 }
 
}
 
<!DOCTYPE html>
 
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <h1>@GetMessage()</h1>
    </body>
</html>

In this example, we call the @GetMessage function which just reads a URL parameter and based on the parameter, it determines what message to display. Notice how the function call is embedded in the markup. To use it, we must proceed it with the @ character which is use to start inline expressions.  To define the function, we use the @functions block directive. In this block we can add functions and properties that can be reused in the page.

The following table shows what query string can be used to load this page and the expected output:

QueryString
Output
Mydomain.com/Page
Hello
Mydomain/Page?typ=sales
Welcome to Sales
Mydomain/Page?typ=support
Welcome to Support

I would suggest using functions on Web Pages for specific functionality on each page. If you want to build functions for common functionality that can be used in several pages, you should add a separate CSHTML page and make the methods static. If you need to reuse blocks of HTML, Razor helpers should be used instead.