ASP.NET MVC 3: HttpNotFoundResult, RedirectResult, HttpStatusCodeResult

Next to the plain old ActionResult we know from the previous versions, we can now make use of some extra helper classes:

  • HttpNotFoundResult
  • RedirectResult
  • HttpStatusCodeResult

You don’t need to be a rocket scientist to figure out what they will do, but I will add a few examples anyway:

HttpNotFoundResult

Where in the past, you had to create a specific view when an item doesn’t exist, you can now make use of the HttpNotFoundResult action:

public ActionResult Details(int id)
{
    Dinner dinner = dinnerRepository.GetDinner(id);
    if (dinner == null)
    {
        //return View(“NotFound”);
        return new HttpNotFoundResult(“Dinner has been deleted or does not exist”);
    }
    else
    {
        return View(dinner);
    }
}

RedirectResult

Logically redirects you to a given url:

public ActionResult Index()
{
    return new RedirectResult(“https://kevinderudder.wordpress.com”);
}

HttpStatusCodeResult

Give a respond with a specific code and description:

Dinner dinner = dinnerRepository.GetDinner(id);
if (dinner == null)
{
    //return View(“NotFound”);
     return new HttpNotFoundResult(“Dinner has been deleted or does not exist”);
}
else if (dinner.Expired)
{
    return new HttpStatusCodeResult(410);
}

ASP.NET MVC 3: ViewBag Property

In the previous versions of ASP.NET MVC, if you wanted to pass data from your controller to your view you had to use the ViewData dictionary:

public ActionResult Index()
{
            ViewData[“DataComingFromTheController”] =
                            “I’m data coming from the controller”;
            return View();
}

In your view:

<%: ViewData[“DataComingFromTheController”]  %>

Because ASP.NET MVC 3 is .NET 4, Microsoft could take benefit of the dynamic keyword to create dynamic properties. So instead of using the ViewData dictionary, you can use the ViewBag property, which makes coding easier:

public ActionResult Index()
{
         ViewBag.DataComingFromTheController
                = “I’m data coming from the controller”;
         return View();
}

and in your view:

@ViewBag.DataComingFromTheController

When we take a closer look on the ViewBag property, we’ll see that it’s a dynamic property (also notice that ViewData still exists):

image

ASP.NET MVC 3: Razor View Engine

One of the reasons why I was (or am) a fan of ASP.NET MVC over ASP.NET is the fact that it feels more like a web technology. The guys of Microsoft did a great job making a platform (ASP.NET) to easily make websites, with viewstate and all the things that we know from ASP.NET:

  • RAD development
  • rich controls
  • familiar model when you are coming from a windows form background

With the arrival of MVC, lots of these concepts have been removed and replaced with new advantages like

  • full control over the rendered HTML
  • Test Driven Development
  • SEO
  • No ViewState

In the release of ASP.NET MVC 3, the did an extra step to make the development of views a lot easier and faster. It’s a minor change and once you get the hang of, it makes a big difference. In ASP.NET (MVC 1 or 2) or let’s say, HTML you always have to work with open and close tags. In the case you want to work with data in your markup it gets worse, you have to type <%= or <%#, … With this new view engine, this code has been replaced with a simple @

If we would compare, for example, the code that Scott Guthrie used in his NerdDinner (with MVC 1 or MVC 2) tutorial and compare this with the razor code this would be the difference:

When I create a View to list the dinners, I can now choose between a ASPX view or  a Razor view engine.

image

And the generated markup code:

ASPX:

<% foreach (var item in Model) { %>
   
        <tr>
            <td>
                <%: Html.ActionLink(“Edit”, “Edit”, new { id=item.DinnerID }) %> |
                <%: Html.ActionLink(“Details”, “Details”, new { id=item.DinnerID })%> |
                <%: Html.ActionLink(“Delete”, “Delete”, new { id=item.DinnerID })%>
            </td>
            <td>
                <%: item.DinnerID %>
            </td>
            <td>
                <%: item.Description %>
            </td>
            <td>
                <%: item.HostedBy %>
            </td>
            <td>
                <%: item.Location %>
            </td>
        </tr>
   
    <% } %>

Razor:

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.ActionLink(“Edit”, “Edit”, new { id=item.DinnerID }) |
            @Html.ActionLink(“Details”, “Details”, new { id=item.DinnerID }) |
            @Html.ActionLink(“Delete”, “Delete”, new { id=item.DinnerID })
        </td>
        <td>
            @item.Description
        </td>
        <td>
            @item.HostedBy
        </td>
        <td>
            @item.Location
        </td>
    </tr>
}

Just compare the yellow tags and you’ll see why I love this syntax. If we take a look at the details page of a dinner you will notice following specific tags:

Tag referencing to the model object that is used on the page

@model NerdDinnerv3.Models.Dinner

Page settings. Because we don’t have a <head> on our page, this is the place where we will set the title and also reference to a layout or masterpage:

@{
          ViewBag.Title = “Details”;
          Layout = “~/Views/Shared/_Layout.cshtml”;
}

And of course the view itself. Mind that I’m typing the ‘@’ character twice, to escape the razor syntax:

<h2>@Model.Title</h2>
<p>
    <strong>When:</strong> @Model.EventDate.ToShortDateString()
    <strong> @@ </strong> @Model.EventDate.ToShortTimeString()
</p>

So, I think you have an understanding of the “razor” view engine, take a minute to play with it, it’s really easy.

BESUG Windows Phone Presentation

Yesterday, Frederik and I gave a presentation for the Belgian Silverlight User Group in Antwerp.

Topics discussed:

  • Windows Phone
  • Launchers and Choosers
  • Application life cycle
  • Navigation
  • Global and local application bar
  • Bing Maps
  • Isolated Storage
  • Push notifications

As promised, we would upload the files, so click here to download them

Thanks for all the visitors and their attention. Also thanks to the “Karel De Grootte” hogeschool for the location

New team, new colors, new blog

After working almost 4,5 years at U2U, I’ve decided to move to another team. I’m now defending the blue colors of Howest.

Although I changed the team, I’m still doing the same things as before, giving training or teaching .NET related stuff like:

  • WPF and Silverlight
  • Windows Phone
  • ASP.NET, ASP.NET MVC, ASP.NET Ajax

Whereas U2U was only Microsoft minded, I now have the chance to work on other stuff (Java, Adobe, … ) as well, which is a perfect opportunity to uplift my skills next to the Microsoft skills.

So If you liked my old blog, please update your favorites or feed to this new blog.

If you have any questions, you can contact me on kevin_derudder@hotmail.com

Posted in About. Tags: . Leave a Comment »