Default route not working

橙三吉。 提交于 2019-12-09 13:03:22

问题


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" }
                );

回答1:


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...




回答2:


Try replacing :

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

with :

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

Edit: tried your code, worked for me. Do you have any other routes defined in your global.asax?



来源:https://stackoverflow.com/questions/10435883/default-route-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!