By default, the route does not work.

Why is this not working?

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 clarify the question more intelligent. How can I get around them?

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

routes.MapRoute(
                    "Summary",
                    "{controller}/{id}",
                    new { controller = "Summary", action = "Default" }
                );
+5
source share
2 answers

How do routes work (by default)?

Go back to the default route, which looks something like this:

routes.MapRoute(

    // Route name
    "Default", 

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

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

);

Try to understand how it works.

  • If you go to /, it will trigger a Indexcontroller action Home; Optional Id has been omitted.

  • If you go to /C, it will trigger a Indexcontroller action C; Optional Id has been omitted.

  • If you go to /C/A, it will trigger a Acontroller action C; Optional Id has been omitted.

  • If you go in /C/A/1, it invokes a Acontroller action Cwith an identifier 1.

, URL- /, /C, /C/A /C/A/1, C - , A - . ? , .

, HomeController SummaryController Show.

/Summary/Show/1 SummaryController.Show(1)


, (/Controller/Id) ?

, , /Summary/1 SummaryController.Show(1).

:

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

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

, Home, Default. Summary, , URL- Summary/{id} . , Show Summary id ; , ...

, Summary, .

:. . , . , , ...

+11

:

new { controller = "Summary", action = "Default" }

:

new { controller = "Summary", action = "Default", id = UrlParameter.Optional }

: , . , global.asax?

0

All Articles