ASP.net MVC Url.Action Link formatting
NickName:Ilayaraja Ask DateTime:2013-03-08T19:06:41

ASP.net MVC Url.Action Link formatting

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

More about “ASP.net MVC Url.Action Link formatting” related questions

ASP.net MVC Url.Action Link formatting

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 Quest...

Show Detail

Jquery thickbox to work with Url.Action link in Asp.net mvc

I want to implement a Jquery thickbox to show an image that is generated from my database in ASP.Net MVC. My link looks roughly like this: <a href="<%=Url.Action("ShowPhoto", "Item", new { i...

Show Detail

Problem by Url.Action in asp.net MVC

I have a problem by Url.Action in asp.net MVC. this is a sample: Url.Action("index", new { page = 1, success = 2}); This code generate this url index?page=1&success=2 In this url there is...

Show Detail

How to use C# nameof() with ASP.NET MVC Url.Action

Is there a recommended way to use the new nameof() expression in ASP.NET MVC for controller names? Url.Action("ActionName", "Home") <------ works vs Url.Action(nameof(ActionName), n

Show Detail

ASP.NET MVC Url.Action adds current route values to generated url

I have seen this question a couple of times here in SO but none of them with any acceptable answer: ASP.NET MVC @Url.Action includes current route data ASP.NET MVC implicitly adds route values

Show Detail

@Url.Action in ASP.NET MVC to action formaction?

I've spent most of the day chasing my tail on this. There are many wonderful suggestions out there, but none that address all my problems: We have an existing website, where someone's linked butto...

Show Detail

How I redirect with link in asp.net mvc?

i used one link in my asp.net mvc project like this way. <a href="<%= Url.Action("DisplayAction", "TempController") %>" title="Display" /> it w

Show Detail

Url.Action in asp.net core

I like the new tag helpers attributes which make readable dynamic html contents. I can create a link by using the asp-controller and asp-action attributes like this: <a asp-controller="Home" asp-

Show Detail

ASP.NET MVC Routing and URL.Action

in one of my asp.net mvc 2 views I am have the following statement window.location.href = '<% = Url.Action("Index","Feature", new {id=""}) %>/' + $("#ProductId").val();

Show Detail

converting URL to Action Link @url.action

I have problem with IIS does not read the URL. Failed to load resource: the server responded with a status of 503 (Service Unavailable) My project is on Asp.net MVC I have used a Jqx grid on my pro...

Show Detail