1

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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I did specify the id when browsing to the page, but not when I defined the link using Url.Action. IMHO, the Url.Action should be building a link to my requirements, not 'hunting' for extra values. That means that I cannot create a link to /foo/bar from /foo/bar/test. I noticed this when I was trying to post a form to /foo/bar/ from /foo/bar/test.
0

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

1 Comment

I don't think it is as I only have one optional url parameter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.