Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# ASP.NET MVC Basics Controllers Using Action Result Types

Aaron Selonke
Aaron Selonke
10,323 Points

MVC ComicGallery Broke when swtiching to the ContentResult action method

Followed line by line when switching the Detail fucntion to an action method, but the site broke afterward. Getting the 404 error.

Here are the two screenshots of the code from the IDE and the 404 error in the browser, What did I miss? http://uploadpie.com/sOAg8 http://uploadpie.com/zNx33

Thanks in advance

2 Answers

Dom Talbot
Dom Talbot
7,686 Points

Hi Aaron

I think it might be because your ComicBookController class isn't set to public.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace ComicBookGallery.Controllers
{
    public class ComicBooksController : Controller
    {
        public ContentResult Detail()
        {
            return new ContentResults()
            {
                Content = "Hello from the comic books Controller"
            }
        }
    }
}
Aaron Selonke
Aaron Selonke
10,323 Points

Yes that was it, Thanks.

Follow up question is that in the file path http://localhost:53286/ComicBooks/Detail Controller = ComicBooks Action = Detail

The routing process knows that the controller is /ComicBooks/ because the class name ComicBooksController? Is that correct?

That is correct. As long as you follow the controllerNameController pattern, MVC will automatically associate the /controllerName/ route with that controller.