Search code examples
asp.netcrudopenrasta

Implementing simple CRUD with OpenRasta and Web Forms


I've been asked to look into OpenRasta as an alternative to MVC ASP.NET at work, and as a starting point I'm trying to replicate the Movies tutorial from the MVC ASP.NET website.

I really like the ReST style of OpenRasta, and so far have got a simple database and a handler for GET based by ID, in the form of

            ResourceSpace.Has.ResourcesOfType<Movie>()
                .AtUri("/movie/{id}")
                .HandledBy<MovieHandler>()
                .RenderedByAspx("~/Views/MovieView.aspx");

I understand that use of POST and DELETE would allow me to create/update and delete items from my database, but unfortunately I'm stumped on how to do the views.

In the OpenRasta documentation it says:

When you use an aspx page as a view in OpenRasta, you essentially create a template to
generate content. As such, postbacks and events are not supported.

I might be being really dumb here, but would I be able to POST and DELETE from an ASP.NET page in the manner required by OpenRasta? I'm using a code-behind page, but that's not something I'm fixated upon.

I'm not that familiar with ASP.NET (haven't done any for ages), so I may be missing something obvious, but would really appreciate some pointers in the right direction.


Solution

  • What this means is that the postback model in asp.net webforms (aka the behavior by which the asp.net webforms infrastructure creates one massive form tag to post back asp.net specific data to a page continuously) is not supported, so any events you may be used to use on webforms controls will not work.

    If you're used to MVC-styled interactions, you know how to use the form tag so you do as usual to create a new movie.

    <form method="post">
      <fieldset>
        <input type="text" name="Name" />
        <input type="submit" />
      </fieldset>
    

    The alternative is to do it in code using the webforms engine

    <% using(scope(Xhtml.Form<Movie>().Post())) { %>
       <%= Xhtml.TextBox<Movie>(_=>_.Name) %>
    <% } >
    

    And your handler code

    public Movie Post(Movie movie) {
      // create the movie instance in your db or whatever
      return new OperationResult.SeeOther { RedirectLocation = movie.CreateUri() };
    }
    

    Code compiles in my head and may need a reality check before being put in a compiler.

    Note that it's probably a good idea to move away from the webforms engine if you can, there are better alternatives (razor, spark, whatever you may decide to plug in).