Working With Microsoft Web Helpers In MVC 3 Razor View

6. March 2011 10:58

API ASP.NET MVC Microsoft 

Building applications with ASP.NET MVC 3 and that too with RAZOR is easy. I believe most of the programmers out there found MVC 3 with RAZOR view engine found it easy. The best things I found in the new RAZOR view engine is the code is pretty much tidy no traditional code behinds and moreover you can work with the server-side code with the start of the @ symbol.
What is a Helper Class?
I am not going to copy and paste the definition from somewhere but I want to write my own thoughts on the helper class. Maybe this is not the best definition but this is how I would like to describe the Helper Class.
A class that minimize the effort incurred in the design, implementation of functionality on your application is called a helper class.
One of the best things that Microsoft incorporated in the framework is the use of the helper classes. Although this is not a new concept, previously also all the programmers are working on their own to create helper classes for the work they are doing to minimize the effort and time spend in writing the same lines of code again and again. Microsoft Web Helpers provides some of the best helper classes that can be used in almost every web application.
How to get Microsoft Web Helpers
To use Microsoft Web Helpers in your project use the below NuGet command:
If you are using Web Matrix then you can start using the helper class right away, but if you have created the application from Visual Studio then you still need the Web Matrix reference in you project. You can add the Web Matrix reference from C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies. Once installed, you can now use it for majority of the complex tasks with ease and in less time. Some of the major use of this helper class is listed below:
Using Web Helpers
Gravatar: To show the gravatar on your web page use the Gravatar class Gethtml() and pass e-mail address as a parameter. If the e-mail id provided, has no Gravatar then the default Garavatar image will be displayed. To get the Gravatar with default Gravatar image:
@Gravatar.GetHtml("prashant@midnightrogrammer.net")
 
You can also have your own default image to replace the default Gravatar image. To show your own default image use the defaultImage: attribute
@Gravatar.GetHtml("prashant@midnightrogrammer.net", defaultImage: "http://midnightprogrammer.net/pics/default.gif")
Twitter: Twitter helper class can be used to show your tweets using the Profile method. This will display the same widget that you can get for youself from the Twitter website. Moreover you can also change the apperance of the widget. To display your tweets you can use your twitter profile name as a parameter in the Profile method.
@Twitter.Profile("prashantmx")
 
You can also search Twitter and display all the reactions. If I have to search about Windows Phone 7 then I will use the keyword #WP7.
@Twitter.Search("#wp7")
 
For Twitter follow button:
@TwitterGoodies.FollowButton("prashantmx")
Tweet Button: The tweet button will help you to tweet your website or blog content. But this time we will use the TwitterGoodies class TweetButton
@TwitterGoodies.TweetButton(TwitterGoodies.DataCount.Horizontal, "This is RAZOR Tweet", "http://midnightprogrammer.net", TwitterGoodies.Languages.English,"prashantmx")
 
You can set the align of tweet counts either vertically or horizontally or none. I have set it to horizontal.
Google / Yahoo Analytics: Insert tracking code in your website directly
@Analytics.GetGoogleHtml("UA-9087")
Same can be done for Yahoo. Just use GetYahooHtml instead of GetGoogleHtml
Facebook: Facebook integration is now much easier. To get the like button use:
@Facebook.LikeButton("http://midnightprogrammer.net")
To show the latest activity feed use the ActivityFeed method. I am going to see the latest activity from Microsoft
@Facebook.ActivityFeed("http://www.microsoft.com")
 
Microsoft Bing Search: If you want to have a search then the best option is to use Microsoft Bing (Because It's Not Google). There are two Bing search boxes that you can use, but the most popular is the Bing Advanced Search Box. Below are the two ways to use the Bing Search box
Normal Search Box
@{
    Bing.SiteTitle = "Midnight Programmer";
    Bing.SiteUrl = "http://midnightprogrammer.net";
}

@Bing.SearchBox()
 
Advanced Search Box
The same code will be used to show the advanced search box, just use the AdvancedSearchBox method instead of SearchBox as done previously
@{
    Bing.SiteTitle = "Midnight Programmer";
    Bing.SiteUrl = "http://midnightprogrammer.net";
}

@Bing.AdvancedSearchBox()
The main advantage of using the advanced search box for Bing is that it will let you search the whole website/blog without taking you to the Bing site and display the search content in the small container with two tabs showing the search result from the website/blog and the other one from the web.
Note: Bing team at Microsoft announced that they will be shutting down siteowner service on April 4th. This will prevent you to use the AdvancedSearchBox feature of Bing on your website/blog or web applications. Here is the quote from Bing's official blog:
As of April 4th, 2011 we will discontinue support for the bing.com/siteowner functionality. If you continue to leverage the feature after this date (April 4th, 2011), users who attempt to query in the search box will be redirected to a 404 page. This means that you will need to move to the Bing API in order to continue to receive web or site search results from us. We have decided to take this step for two reasons.
First, the tool has not been getting a lot of use and while we have some very loyal customers on it, we believe we can provide you better alternatives in the future as we free up resources.
Second, the functionality available through bing.com/siteowner can be replicated with the Bing search API and so it feels a bit redundant.
As has become customary, we will give you a window to migrate off and ask us any questions you may have about this change. We will also provide reminders as we get closer to April 4th. Thank you so much for your support.
 
So this is it. These are some of the helper classes in action that I have described in this post. There are lot of others and I hope that there would be other helper classes would be added in the later releases of Microsoft helper classes for web. You can read more here on web helpers from the below official Mirosoft resources.

Download: DBMatrix.zip (1.13 mb)

Currently rated 5.0 by 7 people

RAZOR: View Engine In ASP.NET MVC 3

6. September 2010 16:26

ASP.NET MVC Visual Studio 

A new view engine is introduced in ASP.NET MVC 3 called RAZOR. You can download Preview 1 from Microsoft's download center and get it running on your machine to check the new features in MVC 3. Just in case if you are thinking that installing MVC 3 will mess your current MVC 2 installation....No, it won't. I came across a Webcamp video where Phil Haack with James Senior demonstrate this view engine. My this post is also based on the same talk.

So when you install MVC 3 Preview 1 on your machine, jump start by creating a new project in Visual Studio 2010. You will not be able to use this view engine with .NETFX 3.5 (SP1).

Now when you select new project and under Web templates you will now see ASP.NET MVC 3 application twice. The one template with (ASPX) is using normal web forms as your view engine and the other one with (Razor) is the new view engine. This is only for preview 1, these options will not be present in the final release of the MVC 3, instead you will have an oprion to select the view engine for your application later, I am not very sure how, but it will be more likely the way you select tests.

Time to check the Solution Explore. No .aspx files only .cshtml

The drawback in Preview 1 is that there is no syntax highlighting and no intellisense. Now that would have made you think "OMG! how am I going to write the code now!?". But Razor is easy and more over it too light-weight, more then I thought before I used it. Check out Index.cshtml, the markup code looks like this.

Time to take a look at Home Controller. Take a look at the Index() action method, the syntax is bit different with the one we have used previously.

 public ActionResult Index()
{      
       ViewModel.Message = "Welcome to ASP.NET MVC!";
       return View();
}

The line ViewModel.Message = "Welcome to ASP.NET MVC!"; is equivalent to:

public ActionResult Index()
{      
       ViewData["Message"] = "Welcome to ASP.NET MVC!";
       return View();
}


The ViewModel basically is of type dynamic which means that its operations will be resolved at runtime. Hover your cursor over ViewModel and Message to know about it.

If you need to show some addtional information related your current project you can set it too with ease. In the Index method you can add antoher line like this which says something about your project:

NOTE: As ViewModel is of Dynamic type, so when you press dot (.) after writing ViewModel don't expect that something will come up and as said before all expressions will be resolved at runtime.

public ActionResult Index()
{
      ViewModel.Message = "Welcome to ASP.NET MVC!";
      ViewModel.AboutApp = "This is a sample ASP.NET MVC 3 Preview 1 application!";
      return View();
}

To show this message on the Index view you will just call it by prefixing it with '@' symbol. To show the value on the page you simply write below line and prefix it with @ symbol(If you say this is simple, I would rather prefer to say it effortless).

@View.Message 
@View.AboutApp

Moving further, add a new class called Contacts (or what ever name you like) with 3 peoperties.

public class Contacts
{
    public string Name{ get; set; }
    public int Age{ get; set; }
    public string Email { get; set; }
}

Moving back to the HomeController add the below lines of code to the Index action method.

var ctc = new Contacts[] {
          new Contacts{Name="Prashant", Age = 23},
          new Contacts{Name="Arvind", Age = 24},
          new Contacts{Name="Max", Age = 23}
};

The view should not be empty this time. And therefore your Index action method return the Contacts we have just created.

public ActionResult Index()
{
ViewModel.Message = "Welcome to ASP.NET MVC!";
ViewModel.AboutApp = "This is a sample ASP.NET MVC 3 Preview 1 application!";             
var ctc = new Contacts[] {
          new Contacts{Name="Prashant", Age = 23},
          new Contacts{Name="Arvind", Age = 24},
          new Contacts{Name="Max", Age = 23}
          return View(ctc);
};
}

This time also Razor makes it simple to view the the data you returned from the controller. In the Index.cshtml you can do it pretty simply and you will still have a pretty looking markup of the view page. Create a list of contacts you have just created. As they are in a set of array, we can then enumerate the array with a foreach loop and show it on the page.

<ul>
@foreach (var c in Model){
<li> My name is @c.Name and I am @c.Age years old. </li>
}</ul>

You can easily get the data in a jiffy without writing a lot of markup. No problem in concatenating the data, the @ symbol and MVC compiler is pretty intelligent and does this for us. So what if we want to add e-mail addresses also. How this view engine will handle this ambiguity? Well let see. Just add an e-mail property to the array of contacts.

var ctc = new Contacts[] {
          new Contacts{Name = "Prashant", Age = 23, Email = "prashant@midnightprogrammer.net"
}

And on the view page the line will be something like this

<ul>
@foreach (var c in Model){
<li> My name is @c.Name and I am @c.Age years old. You can email me at @c.Email.</li>
}</ul>

Hit F5 and see it by yourself. There are lot of features described by Scott Guthrie and Phil Haack on their blog. Well I can go on writing and writing about Razor, so I think I should take a pause now and you can go to below links for more deep information about Razor view engine and to know what's new in the upcoming release of ASP.NET MVC 3.

Recommend Reading:

 No Rating

Diagnostic Check For ASP.NET MVC Application's Problems

12. August 2010 17:08

ASP.NET MVC 

Diagnosing problems in your ASP.NET MVC application is now more easy by using a single file which is included in the MVCFutures package. Download this package from CodePlex and just drag and drop a file called MVCDiagnostics.aspx in the root of your MVC application and thats it.

 

Press F5 and set the URL pointing to the file diagnostics file and you will be able to see a detailed information about your currently running MVC application. The page you see should look something like this.

 

 No Rating

ASP.NET MVC 2 Released

12. March 2010 14:33

ASP.NET ASP.NET MVC Visual Studio 

Microsoft announced the final release of their second and popular ASP.NET MVC framework. This release comes up with some excellent features. Scott Gu's Blog Series on MVC 2 describes the new features and extensibility of this new release.

To list out some new features include:

According to Scott Gu the final release of VS2010 will have in-built support for MVC 2.

Recommend Reading:

Download ASP.NET MVC 2 final release for Visual Studio 2008 and Release Notes from Microsoft Download Center

 No Rating

«123456»