0

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.

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

4 Comments

I did that, and it still send to the following URL: "/Settings/1/1" instead of "/Settings?id=1&siteId=1". Why can I not use multiple params and have to sub them out in a path /1/1?
Perhaps I am misunderstanding what you're after. Why are you looking to use Querystring variables? URL segments are more typical in MVC to pass in parameters to your actions.
K. That is what I was wondering. So it is best and "standard" to use /{param}/{param}? Then I will just forget the querystring params. That is why I am getting so confused by peoples answers on this routing stuff... lol Thanks.
No worries. You can use querystring variables, but yes, typically URL segments are used.

Your Answer

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.