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:

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:

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:

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

public partial class HelloView : FubuPage<HelloModel> {
}

The View itself would contain:

<%=this.Model.Text %>

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

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:

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.

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"