ASP.NET MVC @ Url.Action Includes Current Route Data

Suppose I:

  • Take the route {controller}/{action}/{id} in my Global.asax file.
  • The Foo controller and the Bar(String id) action return a view.
  • A very simple view containing the URL displayed by @Url.Action("bar", "foo") - explicitly NOT specifying id .
  • If I go to /foo/bar/test , the view will show the display URL as /foo/bar/test . I would suggest that it should be /foo/bar , as I did not specify a value for id .

Why would it include the current identifier, even if I did not define it, and is there any way to prevent this (except for the "hard-coding" URL)?

+1
asp.net-mvc-3
Aug 05 2018-11-11T00:
source share
2 answers

You specified a value for id when you put /foo/bar/test in the address bar of the browser. You essentially call the Bar method with the "test" parameter in your foo controller. Your simple view does not matter when viewing directly.

To answer your last question, you may need to re-examine the way you use MVC.

+1
Aug 05 2018-11-11T00:
source share

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

This is the correct answer: stack overflow

0
Jun 20 2018-12-12T00:
source share



All Articles