ASP.NET MVC @Url.Action includes current route data

江枫思渺然 提交于 2020-01-24 20:16:07

问题


Suppose I:

  • Have a route {controller}/{action}/{id} in my Global.asax file.
  • A Controller Foo and Action Bar(String id) returning a view.
  • A very simple View containing a URL rendered by @Url.Action("bar", "foo") - explicitly NOT specifying an id.
  • If I browse to /foo/bar/test, the view will show the rendered URL as /foo/bar/test. I would have expected that it should be /foo/bar as I didn't specify a value for id.

Why would it include the current id even when I didn't specify it, and is there any way of preventing this (apart from 'hard-coding' the URL directly)?


回答1:


You did specify the value for id when you put /foo/bar/test in the browser address bar. You are essentially calling the method Bar with the parameter "test" in your foo controller. Your simple view is irrelevant when you browse directly.

To answer your last question you may need to have a re-look at the way you are using MVC.




回答2:


I believe this is a regression bug in MVC3, as discussed by Phil Haack here: http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx

This is the correct answer: https://stackoverflow.com/a/7133304/27938



来源:https://stackoverflow.com/questions/6950470/asp-net-mvc-url-action-includes-current-route-data

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