1

So I've read about routes in MVC and configured according to what's mentioned in other posts.. So my intention is to generate url as below:

http://domain.com/controller/action/id

with this intention I've configured my RoutesConfig.cs as below:

routes.MapRoute(
      name: "AdminMessage",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Admin", action = "Messages", id = UrlParameter.Optional }
);

Below is what my Controller looks like

[HttpGet]
public ActionResult Messages(string id)
{
    _model = new AdminViewModel();
    _userName = Convert.ToString(HttpContext.User.Identity.Name);
    ViewBag.Page = "Messages";
    _model = PrepareLayoutModel(_userName, "Messages");
    return View(_model);
}

And I use Url.Action to generate url as below:

<a href="@Url.Action("Messages", "Admin", new RouteValueDictionary(new {message.MessageID}))"></a>

But it always generates Url as http://domainname/controller/action?messageID=SDc@#123#2@, instead I want to generate it as http://domainname/controller/action/SDc@#123#2@. So what is the way I can get this done?

2 Answers 2

1

It needs to be

@Url.Action("Messages", "Admin", new { ID = message.MessageID })

Which creates an object with a property named ID (which matches your route) as opposed to your current implemenatation which is using an object property named MessageID which does not match your route so its value is added as a query string value

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

4 Comments

Perfect.. Silly mistakes keep on happening.. :(
Can I ask one more doubt here? How can I get id parameter in Razor view? I tried with Request.QueryString[0] and Request.QueryString["id"] but both the times it returned null.. Will the last part now will be treated as querystring? How can I get this?
Because you have url: "{controller}/{action}/{id}", then the value is added a a route value, not a query string value. But because the parameter in the method is string id, then that parameter will be the value of message.MessageID (and you can also access route values using HttpContext.Current.Request.RequestContext.RouteData.Values["id"]; but that is unnecessary)
Yea.. That's correct.. :) Thanks much for the info.. Have a pleasant time.. :)
1

Try like this:

<a href="@Url.Action("Messages", "Admin", new { id = message.MessageID })"></a>

or even better use the Html.ActionLink helper that will generate an anchor tag pointing to the desired controller action and passing the id route parameter value:

@Html.ActionLink(
    linkText: "", 
    actionName: "Messages", 
    controllerName: "Admin", 
    routeValues: new { id = message.MessageID }, 
    htmlAttributes: null
)

This being said, if you intend to be passing special characters in the route portion of your urls you should be prepared to suffer. In this case I would simply recommend you using query string parameters which can be more than safely url encoded without you suffering. So basically if you do not control the format of this MessageID parameter and narrow it down to basic alphanumeric characters you more than totally don't want it as part of your route segments.

6 Comments

Thanks for the extra info @Darin.. I forgot to mention that I cannot use @Html.ActionLink because there are some other elements which comes inside the a tag... Thanks much for time and help.. :)
plus 20 in different ways.. ;)
I can't think of any viable reason against using standard HTML helpers such as Html.ActionLink. Even if you need to put some of your own crap inside the link text you could always have written a custom HTML helper instead of ending with such terrible spaghetti mixture between HTML DOM markup and server side helpers as attributes of thos DOM elements.
Well, then I have to look into creating a custom HTML helper as my knowledge on them is very limited.. Definitely I will look into it buddy.. :) Thank you.. :)
Oh you definitively should look into creating custom HTML helpers. I can hardly believe how people are even using Razor in real world applications without customizing it.
|

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.