0

I have an odd issue. I have a controller action which takes a couple of optional parameters

Function Index(sectionID As Integer?, title As String) As ActionResult

    Return View()
End Function

I then have added a specific route for this action method so that we get pretty urls for this page

routes.MapRoute( _
        "By_Section", _
        "home/{sectionID}/{title}", _
        New With {.controller = "Home", .action = "Index", .sectionID = Nothing},
        New With {.sectionID = "\d+"}
        )

This all works. However, when I am on a page where the sectionID is set (for example http://localhost/home/index/1/test), the following piece of code produces an odd output.

<%= Url.Action("Index", "Home")%>

Instead of showing http://localhost/home/index as you might expect, it shows http://localhost/home/index/1/test. So it appears it is picking up the sectionID and title from the current url and automatically inserting them into the Url.

How can I prevent this from happening?

Thanks

James

1 Answer 1

1

Yes, this is expected behaviour, the routing system will reuse parameter values from the current request if you haven't provided a new value explicitly. The best option when rendering links is to specify explicit values for all of your routing parameters.

<%= Url.Action("Index", "Home", new { sectionID = (int?)null }) %>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. So how can I force a null parameter because in my case a null sectionID has meaning?

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.