Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Matthew McGuff
Matthew McGuff
4,355 Points

ASP.NET Code Challenge: Not sure how the Content Detail method is not returning the Content method.

I was working on the code challenge below:

Update the provided VideoGamesController.Detail action method to use ActionResult for its return type instead of a string. In the action method, return the result of a call to the base controller's Content method. Keep the content the same by passing the string literal "Welcome to the Video Game Detail page!" to the Content method.

This is the code it starts with:

using System.Web.Mvc;

namespace Treehouse.Controllers
{
    public class VideoGamesController : Controller
    {
        public ActionResult Detail()
        {
            return new ContentResult() { Content = "Welcome to the Video Game Detail page!"};
        }
    }
}

Below is my answer but I get the following Error which I believe I am handling correctly:
Bummer! In the 'Detail' method, are you returning a call to the 'Content' method?

using System.Web.Mvc;

namespace Treehouse.Controllers
{
    public class VideoGamesController : Controller
    {
        public ActionResult Detail()
        {
            return new ContentResult() { Content = "Welcome to the Video Game Detail page!"};
        }
    }
}

My detail method "public ActionResult Detail()" has in its return the ContentResult() which is inherited from ActionResult so creating object here should work as it does int the previous example from the lesson. Any ideas what I am missing here?

3 Answers

Steven Parker
Steven Parker
231,236 Points

When you call a method, the parentheses come after the method name..

Also, any argument(s) being passed to the method will go inside those parentheses. And you won't need new because you're not instantiating a class object, just making a simple function call.

Matthew McGuff
Matthew McGuff
4,355 Points

I now see what I was doing wrong. below is a summary of what I needed to change :

return Content("Welcome to the Video Game Detail page!");

In the example from the instructor I had used what I previous and had the same result but it is no in fact using the method call as instructed. Thanks for the explanation.

using System.Web.Mvc;

namespace Treehouse.Controllers { public class VideoGamesController : Controller { public string Detail() { return "Welcome to the Video Game Detail page!"; } } }

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points

correct answer is

using System.Web.Mvc;

namespace Treehouse.Controllers
{
public class VideoGamesController: Controller
{
public string Detail()
{
return "Welcome to the Video Game Detail page!";

}
}
}