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.

6
  • 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? Commented Nov 14, 2011 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. Commented Nov 15, 2011 at 0: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. Commented Nov 15, 2011 at 0:14
  • @KirkWoll and you're building that statement upon exactly what? i know tons and tons of developers who hates the <asp:*> 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! Commented Nov 15, 2011 at 0: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. Commented Nov 15, 2011 at 0:38

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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 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.
Ostero I love Razor because of it's flexibility!
@kingdango i just like Razor because it has a nice syntax, but i love asp.net for its flexibility :)
I just like being one-up'd on every comment... I'm eager to see what lies ahead. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.