5

I am currently researching view engines and Razor Views have become very interesting to me. I am in process of developing an asp.net 4.0 web forms application. Razor views examples from what I could find are predominantly with MVC applications.

Is it possible to integrate Razor views in to a web forms application? Is it beneficial to do so? The main reason I look to do this is to create a new layer for my applications architecture and possibly a new area that can be tested.

amateur
  • 43,371
  • 65
  • 192
  • 320
  • Razor excels at allowing you to intersperse C# code within HTML. WebForms excels (such as it does) at using XML with custom "tags" that translate into server-side controls that together with the HTML produce the final pure-HTML result. What is your motivation -- what advantages could you forsee -- in using Razor in the WebForms world? – Kirk Woll Nov 14 '11 at 23:49
  • @kirk razor is a nice syntax for mixing code with html and many designer/web developers are clapping in their hands over this. Its a perfect way to maybe lower the threshold for maintaining existing WebForms applications by people who like the Razor syntax. – Pauli Østerø Nov 15 '11 at 00:01
  • @Pauli, you didn't really address my point. When using WebForms, the whole point is that *you should not be incorporating C# code in your code-front*. – Kirk Woll Nov 15 '11 at 00:14
  • @KirkWoll and you're building that statement upon exactly what? i know tons and tons of developers who hates the tags and just want to do easy for-loops and if-statements, and they are so very welcome. I know that WebForms is mostly about Control Trees, Events and Postback which WebPages has none of, but developing is all about using the tool that does the job best, and if a small area of a large WebForms application is best maintained by some dude who prefers Razor, then that's what you should do! – Pauli Østerø Nov 15 '11 at 00:22
  • @Pauli, but you are describing ASP.NET/MVC. Why on earth shoehorn that functionality into WebForms which you yourself confess is "mostly about Control Trees, Events and Postback." If I didn't think you were being disingenuous with your question, "and you're building that statement upon exactly what?" I would point you to the scores of official MSDN documentation that explains how they think you should be developing WebForms applications -- and trust me, it's not a world you have been describing. – Kirk Woll Nov 15 '11 at 00:38
  • Wait, what! Where did i say anything about Model and Controllers !? Razor has *nothing* to do with Mvc, except that they happen to like it so they use it as a way to write their views. Razor is just another way of writing templates which many people happens to like and you should feel free to use it in any project you feel it might fit it. Just like the Mvc-people liked it so much, many people writing Webforms also happens to like it. 1. is it possible, yes. 2. is it beneficial, depends. If you write Razor classes 10x faster than normal c# webforms, then yes, its beneficial. – Pauli Østerø Nov 15 '11 at 08:33

1 Answers1

3

Of course you can! By using the WebPages project from Microsoft you can load razor-classes the same way you would normally load an UserControl, by giving a path to a class/razor file. What you get back is an instance of a WebPage that you can execute and that will give you a string you can print out on your page.

I've done this myself, implemented Razor functionality for Composite C1 CMS and the sourcecode for it is freely available from http://compositec1contrib.codeplex.com/. I'll highlight the important parts here.

Make sure you have a build provider for .cshtml files registered in the web.config

Make sure you have the necessary system.web.webPages.razor configuration setup

Instantiate an instance of a .cshtml file like this var webPage = WebPage.CreateInstanceFromVirtualPath(_relativeFilePath); (see doc)

Get the output of the Razor class like this

var httpContext = new HttpContextWrapper(HttpContext.Current);
var pageContext = new WebPageContext(httpContext, webPage, null);

var sb = new StringBuilder();
using (var writer = new StringWriter(sb))
{
   webPage.ExecutePageHierarchy(pageContext, writer);
}

string output = sb.ToString();

Just output the string on your WebForms Page

Pauli Østerø
  • 6,878
  • 2
  • 31
  • 48
  • You can integrate a Razor view into a web application but not easily into an existing WebForm. In other words, you can have a web application that contains both Web Form pages and Razor pages but it's a lot more complicated to have a Web Form contain Razor parts. @klabranche provided a great link from Hanselman on the subject. – kingdango Nov 14 '11 at 23:58
  • 1
    @kingdango since all Razor classes in the end only outputs strings, it makes perfect sense, and is very easy to instantiate, execute and write the output of a Razor class at any given place in a WebForms application. – Pauli Østerø Nov 15 '11 at 00:03
  • Ostero I love Razor because of it's flexibility! – kingdango Nov 15 '11 at 00:09
  • @kingdango i just like Razor because it has a nice syntax, but i *love* asp.net for its flexibility :) – Pauli Østerø Nov 15 '11 at 00:13
  • I just like being one-up'd on every comment... I'm eager to see what lies ahead. :-) – kingdango Nov 15 '11 at 18:07