Using Web Pages Helpers in ASP.NET Web Forms
I have already written quite extensively about WebMatrix and ASP.NET Web Pages. The new product has provoked quite a bit of discussion at both ends of the spectrum: some (like me) love it, and others hate it. However, it has spawned the Razor View Engine which has definitely found near universal favour among the ASP.NET MVC crowd. What about using Razor within a Web Forms web site? There seems little point in just replacing your ASPX files with VBHTML files or CSHTML files. You may as well create a Web Pages site instead, but you might want to display Web Pages helpers in your existing ASPX files. ASPX files don't understand Razor syntax. If you tried adding some, it will just render as text. Maybe a User Control is the answer? Nope. That won't work either. But you can use an Iframe or jQuery.
In Visual Studio, create a new ASP.NET Web Site. If you have installed MVC 3 RC with the VS tools, you will see an option to create a Razor site. That's not what you want. You want a straightforward Web Forms site. Now you need to get the Microsoft.Web.Helpers library which is available via the NuGet Library Package Manager. Right click on the project name and choose Add Library Package Reference.
Search for "web helpers" and click install when the microsoft-web-helpers package is found:
Add a Text file (.txt) to the site, but rename it Twitter.cshtml. Once you have done that, add the following code to it:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> </head> <body> @Twitter.Profile("mikesdotnetting") </body> </html>
Change the Default.aspx file that came with the Web Forms site template to include an iframe pointing to your CSHTML file so that it looks like this:
<p> To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>. </p> <iframe src="/Twitter" frameBorder="0" height="400" width="270" scrolling="no"></iframe> <p> You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409" title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>. </p>
Now if you run the site, my Twitter feed will appear within it:
Make sure that the jQuery library which resides by default in the Scripts folder is referenced in the MasterPage:
Then add the following to Default.aspx:
Now when you run the page, you can see both the Twitter feed, and a file upload:
Obviously, if you are going to deploy this site, you need a host that supports ASP.NET 4.0. If your hoster meets that requirement, but doesn't support WebMatrix or MVC 3, you will have to add some additional dlls to your bin folder. These are:
Once you have installed WebMatrix, you should find these in Program Files -> Microsoft ASP.NET -> ASP.NET Web Pages -> v1.0 -> Assemblies.
You can download the sample here.
Currently rated 4.67 by 15 people
17 November 2010 15:00
Last Updated: 21 November 2010 08:31
Posted by: Mikesdotnetting
Total Views to date: 35998
Unfortunately, something went wrong and your message or comments have not been submitted successfully. I'll try to fix whatever the problem is as soon as I can.
Thanks for your comments. They have been successfully sent to me. It may take a while before I review them for publishing, so please check back again.