Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default route not working

Why doesn't this work?

Route:

routes.MapRoute(
                "Summary",
                "{controller}/{id}",
                new { controller = "Summary", action = "Default" }
            );

Controller:

public class SummaryController : Controller
    {
        public ActionResult Default(int id)
        {
            Summary summary = GetSummaryById(id);

            return View("Summary", summary);
        }
    }

URL:

http://localhost:40353/Summary/107

Error:

Server Error in '/' Application.

    The resource cannot be found.

    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

    Requested URL: /Summary/107

    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.225

Update:

Let me update the question with a more intelligent one. How can I have both of these?

routes.MapRoute(
                    "Home",
                    "{controller}",
                    new { controller = "Home", action = "Default" }
                );

routes.MapRoute(
                    "Summary",
                    "{controller}/{id}",
                    new { controller = "Summary", action = "Default" }
                );
like image 942
birdus Avatar asked May 03 '12 16:05

birdus


1 Answers

How do routes work (by default)?

Let's get back to the default route, which is somewhat like this one:

routes.MapRoute(

    // Route name
    "Default", 

    // URL with parameters
    "{controller}/{action}/{id}", 

    // Parameter defaults
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } 

);

Let's try to understand how this one works.

  • If you access /, it will call the Index action of the Home controller; the optional Id was ommitted.

  • If you access /C it will call the Index action of the C controller; the optional Id was ommitted.

  • If you access /C/A it will call the A action of the C controller; the optional Id was ommitted.

  • If you access /C/A/1 it will call the A action of the C controller with id 1.

So, that route allows any URLs of the form /, /C, /C/A and /C/A/1 where C is a controller and A is an action. What does this mean? This means that you don't necessarily have to specify your own routes.

So, without routes you could just have a HomeController and a SummaryController and add an action to that last controller called Show.

Then /Summary/Show/1 would call SummaryController.Show(1)


What if I want to have a shorter route (/Controller/Id) for a controller?

Let's suppose we do want to map routes such that /Summary/1 calls SummaryController.Show(1).

Here is the correct form:

routes.MapRoute(
    "Summary",
    "Summary/{id}",
    new { controller = "Summary", action = "Show" }
);

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Note that we have changed the Home route to look like the Default route. Now we've added a Summary route where we tell that URLs of the form Summary/{id} will trigger that route. When they do, it calls the Show action of the Summary controller and pass along id as a parameter; which is exactly what you want...

Also note that we need to place the Summary route first such that it gets precedence.

Caution: You will not want to create a new route for every single action you create. You also don't want all your actions to be in the same controller. Consider rethinking your approach if one of these is the case, so that you don't end up with problems later...

like image 131
Tamara Wijsman Avatar answered Oct 29 '22 19:10

Tamara Wijsman