Question

I have the following action result:

        public ActionResult Index(int id, int? siteId)
        {
            //code here....
            return View(object);
        }   

I have the following route mapping as follows:

       routes.MapRoute(
            name: "SettingsRoute",
            url: "Settings/{id}/{siteId}",
            defaults: new
            {
                controller = "Settings",
                action = "Index",
            }
        );

What do I need to do, so the url will be "Settings?id=1&siteId=133" instead of the format "Settings?id=1" durring initial load. Then to select a site it builds the URl "Settings/1/133".

I am using the following actionLink to create this:

<li>@Html.ActionLink(site.Name, "Index", "Settings", new { id = Model.SettingsEnvironment.EnvironmentID, siteId = site.SiteID }, null)</li>

I can't seem to get the routing down right. Any help would be appreciated. Thanks.

Was it helpful?

Solution

You need to set your optional URL parameter:

  routes.MapRoute(
        name: "SettingsRoute",
        url: "Settings/{id}/{siteId}",
        defaults: new
        {
            controller = "Settings",
            action = "Index",
            siteId = UrlParameter.Optional
        }
    );

ref: http://haacked.com/archive/2010/02/12/asp-net-mvc-2-optional-url-parameters.aspx/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top