2

I'm trying to build URL in razor view that will pass 2 RouteData.Values to my action.

This is the code:

<input type="button" value="View" onclick="location.href='@Url.RouteUrl("ViewMessage", new { threadId = 955, messageId = 11 })'" />

In Global.asax I defined the following:

routes.MapRoute(
"ViewMessage", // Route name
"{controller}/{action}/{threadId}/{messageId}", // URL with parameters
new { controller = "Board", action = "Messages", threadId = UrlParameter.Optional, messageId = UrlParameter.Optional } // Parameter defaults

);

Why my url looks like this:

http://localhost/Site/Board/Messages/955?messageId=11

and not like this:

http://localhost/Site/Board/Messages/955/11

I understand that I can use string.Format, but rather to find MVC helper solution

2 Answers 2

1

try the following route

routes.MapRoute(
"ViewMessage", // Route name
"{controller}/{action}/{threadId}/{threadId}/{messageID}", // URL with parameters
new { controller = "Board", action = "Messages", threadId = UrlParameter.Optional, messageId = UrlParameter.Optional } // Parameter defaults
Sign up to request clarification or add additional context in comments.

Comments

1

My guess would be: if both threadId and messageId are optional, how would the following URL be resolved: http://localhost/Site/Board/Messages/955?

Try removing UrlParameter.Optional from threadId (and potentially using different route for that).

2 Comments

well, threadId is optional as well, but still it does included as route data
Does removing optional from threadId help? I can explain my opinion, but if it is wrong then there is no point.

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.