Skip to content
simonthorogood edited this page Dec 18, 2010 · 8 revisions

OpenRasta comes out of the box with support for using the asp.net webforms view engine as a codec.

OpenRasta’s WebFormsCodec supports any view that can be rendered by ASP.NET. More specifically, OpenRasta first tries to locate the path provided in the registration, and then execute it, as long as it’s an IHttpHandler implementation.

Registering the codec

Whenever you want to render a resource using the webforms engine, you can use one of two ways to register your views. Note that any aspx page is supported.

ResourceSpace.Has.ResourcesOfType<Customer>()
                 /* your handler declaration */
                 .AndRenderedByAspx("~/Views/Customer.aspx");

Alternatively, you can use the full syntax.
ResourceSpace.Has.ResourcesOfType<Customer>()
                 /* your handler declaration */
                 .AndTranscodedBy<WebFormsCodec>(new { index = "~/Views/Customer.aspx" });

In the full syntax, the anonymous type is used to defined the various views available for a resource URI.

Whenever a URI associated with the codec is accessed, the default view is rendered. This is the one called index or default in the anonymous type declared in the TranscodedBy instruction.

To see how to build a system supporting multiple webforms views for one resource, you can read the Using Multiple WebForm Views with a Resource tutorial.

Implementing aspx pages

Using existing pages

Any aspx page can be used as a view when using the WebForms view engine. The WebForms view engine will always initialize the pages through the IDependencyResolver. As such, you’ll be able to take dependencies registered in your container of choice in your pages without any additional work.

One such dependency is the resource returned by handlers for rendering. To automatically get the resource, you only need to define a property of the correct type.

public Customer Resource { get; set; }

Using the OpenRasta base types

OpenRasta comes with base types you can use in your pages. By using those types, you get support for rich markup and a Resource property already defined. See Creating Views with no Code Behind for a fuller tutorial.

For aspx pages, you can use the OpenRasta.Codecs.WebForms.ResourceView and OpenRasta.Codecs.WebForms.ResourceView<T> as the base class.

If you use a code-behind backed page, you can change the base class from Page to one of those types.

public partial class CustomerPage : ResourceView<Customer>
{
...
}

Alternatively, you can use aspx pages without code-behinds. To add such a page manually, you can create the aspx file and use the following syntax.
<%@ Page Language="C#" Inherits="OpenRasta.Codecs.WebForms.ResourceView@1[[Demo.Resources.Customer]]" %>

Note that because webforms don’t understand C# or VB way of writing generic types, you have to use the CLR notation for a generic type name.

OpenRasta supports two additional ways of defining the base type. For this additional support to be enabled, you’ll have to modify the @` tag in your web.config document.

<configuration>
  <system.web>
    <pages pageParserFilterType="OpenRasta.Codecs.WebForms.OpenRastaPageParserFilter, OpenRasta.Codecs.WebForms">
  </system.web>
</configuration>

Once the parser is in place, you can use a shorthand definition. The previous example could be rewritten as such.

<%@ Page Language="C#" Inherits="ResourceView(Customer)" %>

Visual studio will recognize the base page and gives you full intellisense on the Resource property.

If you’re a Resharper user, this notiation will not be recognized. You can use the Resharper friendly alternative:

<%@ Page Language="C#" Inherits="ResourceView<Customer>" %>

OpenRasta will automatically import the namespaces defined in your web.config/configuration/system.web/namespaces section when compiling the pages.

Using UserControls

Discussion about Xhtml.RenderUserControl()

Using subresource rendering

Discusson about Xhtml.RenderResource(Uri)

Clone this wiki locally