TECHNOLOGY QUESTION -DETAILS WITH ANSWERS: (1)
ASP.NET MVC How to add optional parameter in controller
I need to add additional parameter to the controller so that it appears in the GET string parameters in the browser.
Question ID:000032 Submitted by: Kuldeep Raj on 7/12/2017 Associated Tags: ASP.NET
Answered By Answer - ID: 00037
Ramasawamy On: 7/13/2017
Answer For adding an extra /optional parameter in the controller, so that it appears as GET parameter in the browser, you need to first make certain modification in RouteConfig.cs file. Add optional parameter in the routing configuration.

Edit the file like this: -
 
public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(name: "Default",
                url: "{controller}/{action}/{id}/{NewParam}",
                defaults: new { controller = "Home", action = "Index", 
id = UrlParameter.Optional, NewParam = UrlParameter.Optional } );
 }


And when you invoke the method from Controller, add NewParam and pass the desired value like the following: -
 
@Html.ActionLink("DisplayText", "ControllerMethod", 
"ControllerName", new { id = idValue, NewParam = "NewParamValue" }, 
new { @class = "lead" })



Note:You need to create the values at runtime and pass as required by your program.

Provide Your Answer