Click here to Skip to main content
15,883,901 members
Articles / Web Development / ASP.NET

HW'in it with FubuMVC

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
16 Jun 2011CPOL2 min read 12.2K   4   1
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)


Written By
Software Developer GeekSoft
Lithuania Lithuania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Monjurul Habib17-Jun-11 10:23
professionalMonjurul Habib17-Jun-11 10:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.