Michael
Michael

Reputation: 13636

How to pass parameters in the string?

Here is my route:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

Here is my action:

public ActionResult Index(string id)
{
    try
    {
        return View();
    }
    catch (Exception)
    {
        return View("Error");
    }
}
}

When the page is loading I get this url:

http://localhost:2326/

Now I need to pass parameters to Index action:

    http://localhost:2326/"123456"

When I make this I get error.

I guess there is some problem with routing.

How can I pass parameter to the Index Action?

Upvotes: 0

Views: 46

Answers (1)

Stefan Herijgens
Stefan Herijgens

Reputation: 386

You can pass multiple parameters using:

http://localhost:2326/?id=123456&param2=text&param3=something

or

http://localhost:2326/Sensor/Index/123456/Param2/text/Param3/something

And of course you need to add an extra route to handle the amount of parameters

  routes.MapRoute(
      "MoreParams",
      "{controller}/{action}/{id}/{param2}/{param3}",
      new { controller = "<controllername>", action = "Index", id = "", param2 = "", param3 = "" }
  );

and a method.

Upvotes: 1

Related Questions