views:

110

answers:

3

On this page:

http://nerddinnerbook.s3.amazonaws.com/Part4.htm

After the controller is added, I can browse to http://localhost:xxxx/dinners and it works as expected. My question is how does it know to use "Dinners"? Where is "Dinners" located? My controller is named DinnersController so how did the word Dinners become meaningful. I don't see it in my Linq to SQL or anywhere else. I'm sure I'm overlooking something obvious.

Here is the code:

    //
        // HTTP-GET: /Dinners/

        public void Index()
        {
            Response.Write("<h1>Coming Soon:

Dinners"); }

        //
        // HTTP-GET: /Dinners/Details/2

        public void Details(int id)
        {
            Response.Write("<h1>Details DinnerID:

" + id + ""); }

Where is "Dinners" coming from?

Thank you for any help.

EDIT: I read further in the article before I posted and saw about the global.asax, but I don't understand how it mapped to dinners with this:

 public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }
+4  A: 

ASP.NET MVC favors Convention over Configuration. Meaning it will look for a controller with a Controller suffix and not include it as part of the URL and only include the prefix to Controller. So if you have HomeController you could visit /Home/ just as DinnersController means /Dinners/. This happens as part of the ASP.NET MVC framework itself.

If you look at the default route in Global.asax you'll see it uses a format for the URL that looks like...

"{controller}/{action}/{id}"

This means take the name of the controller and the name of the action and point the request to that method.

So for DinnersController Index action method it would look like /Dinners/Index.

Chad Moran
so it just cuts off the Controller in the name DinnersController and thus gives me Dinners?
johnny
Yes, it only uses the prefix to Controller.
Chad Moran
Thank you. I must be missing that in the article. I'll have to re-read it.
johnny
+2  A: 

I don't have the sample in front of me, but I suspect the routing (see Global.asax) is set up to use Dinners as the default controller name; the specific line responsible would look something like

new { controller = "Dinners", action = "Index", id = "" }

Edit: Re-reading the question, this doesn't quite answer it; the issue you were confused over is that /dinners was resolved to the DinnersController; not any default behaviour. For that case, Chad's answer is right, and as you said - the framework basically chops off the Controller part and works out what name to map.

Rob
+1  A: 

This is part of the "implicit configuration" (Convention vs. Configuration) that MVC gives you via its default routes.

When you go to "www.mysite.com/dinners" MVC will automatically see the "dinners" in the URL and will look for a controller named "DinnersController" and call the "Index" method. If you went to "www.mysite.com/dinners/details", MVC would look for the "DinnersController" and call the "Details" method.

If you look at ruby on rails, it behaves in much the same way (which is almost certainly where Microsoft got this from).

Eric Petroelje