Nerd Dinners Controllers

On this page:

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

After adding the controller, I can go to http://localhost:xxxx/dinners

and it works as expected. My question is, how does he know to use "Dinners"? Where is "Dinner" located? My controller is called DinnersController because Dinners has become meaningful. I don't see it in Linq for SQL or anywhere else. I'm sure I'll be missing 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 "Lunch" located?

Thanks for any help.

EDIT: I read further in the article before I posted and saw about global.asax, but I don't understand how it was displayed at 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);
        }
    }

      

0


a source to share


3 answers


ASP.NET MVC maintains a configuration convention. This means it will look for a controller suffixed with Controller and not include it as part of the url and only include the prefix in the controller. So if you have a HomeController, you can 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 can see that it uses a format for a URL that looks like ...

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

      



This means the controller name and action name and specify the request for this method.

So, for the DinnersController index action method, it will look like / Dinners / Index.

+4


a source


I don't have a pattern in front of me, but I suspect that the routing (see Global.asax

) is configured to use Dinners

the controller as the controller name; the specific responsible line would look something like this:

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

      



Edit: After rereading the question, this doesn't quite answer it; the problem you were confusing is what /dinners

was resolved for DinnersController

; not the default behavior. In this case, Chad's answer is right, and as you said, the structure basically strips off the part Controller

and determines which name should be displayed.

+2


a source


This is part of the "implicit configuration" (Convention versus configuration) that MVC provides you in its default routes.

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

If you look at rubies on rails, it behaves in much the same way (which Microsoft almost certainly has).

+1


a source







All Articles