Wrong MVC route matched. Asp.net

牧云@^-^@ 提交于 2019-12-13 03:06:58

问题


This is my route registration code:

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "course_list",
            "course/list",
            new { controller = "course", action = "list" }
        );

        routes.MapRoute(
            "course_view",
            "course/view/{id}",
            new { controller = "course", action = "list", id = UrlParameter.Optional }
        );

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

I have a link /course/view/87

And the route that is matched is /course/list

Can anyone explain why?

Thank you

UPDATE:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "course_list",
            "course/list",
            new { controller = "course", action = "list" }
        );

        routes.MapRoute(
            "course_view",
            "course/view/{id}",
            new { controller = "course", action = "view", id = UrlParameter.Optional }
        );

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

But I'm still getting the same issue. When i visit: /course/view/87 i get a 404 error.


回答1:


It appears that your route for course/view/{Id} has a 'list' action. I expect this is a typo.




回答2:


Adding these routes to an empty Asp.Net Mvc 4 project and using routedebugger (http://nuget.org/packages/routedebugger/), I get a Matched Route of "course/view/{id}". You should use routedebugger locally to see what is going on. The above code seems to be fine.




回答3:


The button element is treated as submit button (i.e.: type="submit" if not default type attribute is set). Therefore, the browser initiated a post request, which no route satisfied, since all my actions are get(s).

Thank you all for your time.



来源:https://stackoverflow.com/questions/15510717/wrong-mvc-route-matched-asp-net

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