0

I want to get the parameter from a url as below:

www.domain.com/Controller/Action/Parameter

In my controller:

public ActionResult Action()
{
 //some codes
}

User enters url as above and should navigate the the specific controller. How do I get the 'Parameter' in the Action of my controller?

asked Jan 21, 2016 at 4:48
1
  • Change you method to include a parameter - e.g. public ActionResult Action(string id) assuming your using the default route (in your example, the value of id will be "Parameter") Commented Jan 21, 2016 at 4:50

3 Answers 3

1

You need 2 things: 1) add parameter to your action, ie: Action(string param, ...)

2) configure the routing to tell mvc how to map route data to parameters. Default mvc route is configured to map /## to "id" parameter, but you can add more routes to map other parameters as you need. For example add this line to your App_Start/RouteConfig.cs file:

routes.MapRoute( name: "CustomRoute",
url: "{controller}/{action}/{param} );

note that "{param}" match the name of your action parameter.

answered Jan 21, 2016 at 5:13
Sign up to request clarification or add additional context in comments.

Comments

0

Example

public ActionResult Action()
{
 string param1 = this.Request.QueryString["param"];
}
answered Jan 21, 2016 at 4:51

Comments

0

When someone enter url like the example above, you can do this to get the segment value in your controller.

URL: www.domain.com/Controller/Action/Parameter

Controller:

public ActionResult Action()
{
 string parameter = this.Request.Url.Segments[segmentIndex];
 //The segmentIndex in the url above should be 3 
}
answered Jan 21, 2016 at 8:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.