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# ASP.NET MVC Basics Controllers Overview of URL Routing

How does ComicBooks get picked up in the url?

How does ComicBooks get picked up in the url when the class is called ComicBooksController?

5 Answers

Steven Parker
Steven Parker
229,744 Points

:point_right: The routing determines the code to use from the URL.

The first part of the URL after the site name indicates the controller. The routing looks for a controller module with that name and "Controller" added to it.

The next part of the URL indicates the action. The routing will then look for a method inside the controller that has that name.

Thanks for the help.

Got it! I think it was just an spelling error. Thanks for the help!

I am currently using VS 2017 and i am having issues viewing the web page when i compile the same code. Is there something that has changed in the naming conventions of Controllers for VS 2017? I still get the same 404 error when i change the directory to add /ComicBooks/Detail

Michael Lauridsen
Michael Lauridsen
10,321 Points

Hi Juan,

It should still work. In fact I just tested it, and it does.

Are you sure you named your controller "ComicBooksController" and your method "Detail"? It might be a simple spelling error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ComicBookGallery.Controllers
{
    public class ComicBooksController : Controller
    {
        public string Detail()
        {
            return "Hello world from the comic books controller!";
        }
    }
}

Why do you need the word "controller" in the class name. I know he mentioned that you need it so .net knows it's a controller but wouldn't the inheritance from the Controller class tell it that?

Kevin Gates
Kevin Gates
15,052 Points

My guess: it may be an efficiency thing. Faster for a computer to scan the names of files versus scanning the inside of all the documents to find a class that happens to inherit from Controller.

I just asked this question in the last video but It's not clear to me - So I'm understanding it this way if I make a controller called ILoveApplesController that points to the Base Controller then my URL would be /iloveapples/detail ? and this happens because the program is looking for the controller name with the "Controller " name added to it with no spaces?