Creating the Simplest Possible ASP.NET Razor Web Page

I was exploring creating ASP.NET Razor Web pages using Visual Studio. A problem for me was that using the built-in VS ASP.NET Razor template generated an enormous amount of files. The template created a site with an About page, a Login page, and all kinds of other things. I wasn’t sure what was required and what was optional. So I set out to create the simplest possible minimal Razor page.


First I launched VS in administrator mode (see note at end of this post). Annoyingly, VS has two different ways to create Web sites: File | New | Project | C# | Web, and also File | New | Web Site | C#. The two approaches are very different, but to cut to the chase, I prefer File | New | Web Site | C# | Empty Web Site (basically working with raw files rather than creating a binary Assembly that must be deployed).

In the “New Web Site” dialog I specified HTTP as the Web location because I had Internet Information Services set up on my machine, and hxxp://localhost/TestWebSiteProject as the name — even though I started with File | New | Project, the New Web Site approach does not generate a .csproj file.

After clicking OK, I got an almost empty Web site — only files Web.config and Web.Debug.config were created. In the Solution Explorer window, I right-clicked on the bolded project name and selected Add | Add New Item from the context menu. I looked for an ASP.NET Razor page or anything with a .cshtml file extension but there wasn’t any such item. So, instead, I added an empty text File and accepted the default TextFile.txt name, then right-clicked on the file name and changed it to Hello.cshtml.

To test, I added basic HTML, but left out any Razor server-side markup. I hit F5 to validate and test my simple page and got an error: “Could not determine which version of ASP.NET Web page to use.”

After a little research I added some code into the Web.config file:

  (add key="webPages:Version" value="2.0"/)

Note: I replaced less-than and greater-than in the XML above with parentheses so my blog post software stays happy.

I saved all my files (CTRL-SHIFT-S) and hit F5 and the page loaded and displayed in Internet Explorer.

Finally I added some C# Razor markup, saved, and the simplest possible ASP.NET Razor Web page worked.

  (h3)Hello from Razor(/h3)
  @{ var theTime = DateTime.Now; }

  (p)The time is:  @theTime (/p)

Note: I like to launch Visual Studio from the Run command box. When doing any Web work, VS must run in administrator mode. To do that, I went to devenv.exe (at C:, Program Files(x86), Microsoft Visual Studio 12.0, Common7, IDE and then right-clicked and selected “Troubleshoot compatibility”. Next I clicked on “Troubleshoot program”, and checked the “The program requires additional permissions” and then clicked through the screens, being sure to save the changes.

This entry was posted in Miscellaneous. Bookmark the permalink.