Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

HW'in it with FubuMVC

5.00/5 (1 vote)
16 Jun 2011CPOL2 min read 12.4K  
A few tricks in using FubuMVC

After a little discussion on FubuMVC's default conventions, we finally approach our shiny goal: force our browser to display a certain phrase (that looks strangely familiar).

Displaying a simple string

This is simple: add a class called SayController, and an action method that returns a string:

C#
public string HelloString() {
    return "Hi there World!";
}

Now let's browse to /Say/HelloString, and we'll see this string in our browser. By default, Fubu will detect the string output type and set the response type to text/plain.

Constructing an HTML document

Next, we'll use the HtmlTags library in order to construct an HTML document:

C#
public HtmlDocument HelloDoc() {
    var doc = new HtmlDocument {Title = "Hi!"};
    doc.Body.Text("Hello World!");
    return doc;
}

We can see the result at /Say/HelloDoc. Fubu sends the result to the browser, converting it to a string and setting the response type to text/html.

Using a View (finally!)

First, we create a View called HelloView. It's going to be a WebForms view, and it's going to have a code-behind file. Sorry. Spark views are supported (haven't tried it yet), Razor -- not yet.

Our View is going to be strongly typed, and inherits from FubuPage<HelloModel>. We're going to create the model as well:

C#
public class HelloModel {
    public string Text { get; set; }
}

public partial class HelloView : FubuPage<HelloModel> {
}

The View itself would contain:

XML
<%=this.Model.Text %>

By our conventions, the View is matched by the output type of our action method:

C#
public HelloModel HelloView() {
    return new HelloModel {Text = "Hi!"};
}

Browsing to /Say/HelloView, we see our View and the model value.

Using a static View

We have used a Controller without a View, and a View with a Controller. Can we use a View without a Controller? Easy! Create a WebForm view called ControllerlessView, and add the following line to your conventions:

C#
Views.RegisterActionLessViews(type => type.Name == "ControllerlessView",
  chain => chain.Route = new RouteDefinition("ShowMe/MySweetView"));

Then, navigate to /ShowMe/MySweetView, and you'll see your (sweet) View.

This post was too short, wasn't it? Almost brief. Sketchy. Also, no silly jokes here. Well, if I get some sleep tonight, tomorrow I'll try to figure out how to use NHibernate with FubuMVC, and what the FastPack library can offer.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)