I'm very new to ASP.net and recently converting from asp.net to asp.net MVC projects.
I have one basic question in asp.net MVC Url.Action.
I have one ActionResult
method with two parameters QuestionId, Response
in my controller, and routing configuration as follows,
public class QuestionController : Controller
{
public ActionResult Answer(int QuestionId, string Response)
{
..do something.....
}
}
and routing configuration mapped as
routes.MapRoute(
name: "QuestionAnswer",
url: "{controller}/{action}/{QuestionId}/{Response}",
defaults: new { controller ="Question", action = "Answer", QuestionId = 0, Response = string.Empty}
);
Calling the action Link in html page from the below syntax
@Html.ActionLink("Answer is 1", "Answer", "Question", null, null, null, new RouteValueDictionary(new {QuestionId = 10, Response = "1" }), null)
it forming URL Like /Question/Answer?QuestionID=10&Response=1, this url match the routing defintion and calls the Action properly. Everything is fine.
But my actual requirement is these parameter names should not be exposed. It should be like
/Question/Answer/10/1
I don't want to manually format this link with the help of String.Format
since my site may placed inside a virtual directory not as default web site.
Copyright Notice:Content Author:「Ilayaraja」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15292532/asp-net-mvc-url-action-link-formatting