错误的MVC路由匹配

错误的MVC路由匹配

本文介绍了错误的MVC路由匹配. Asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的路线注册代码:

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


我有一个链接/course/view/87


I have a link /course/view/87

匹配的路线是/course/list

And the route that is matched is /course/list

任何人都可以解释原因吗?

Can anyone explain why?

谢谢

更新:

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

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

但是我仍然遇到同样的问题.当我访问:/course/view/87时,我收到404错误.

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

推荐答案

将button元素视为提交"按钮(即,如果未设置默认类型属性,则为type ="submit").因此,浏览器启动了一个发布请求,由于我所有的动作都是get(s),因此路由没有被满足.

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

谢谢大家的光临.

这篇关于错误的MVC路由匹配. Asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 10:49