Turp
Turp

Reputation: 708

MVC String in ActionResult

I am using the default Controller named "Home".

I have the following ActionResult:

public ActionResult SetID(string ID)
{
    int? result = MyGateway.GetAccountByID(ID);
    Common.IDNum = result;
    return View("Index","Home");
}

I would like to pass the IDNumber like:

http://localhost:3314/Home/SetID/AA3420

...and not like...

http://localhost:3314/Home/SetID?ID=AA3420

The link above is beginning generated by a view which returns a list. To select a record, they click "Select" which is the link above. I am currently setting the "AA3420" to a Session Variable located in a CS file elsewhere called Common.cs.

How can I get my URL to look like: http://localhost:3314/Home/SetID/AA3420?

Upvotes: 2

Views: 964

Answers (2)

jenson-button-event
jenson-button-event

Reputation: 18961

By Registering the following route in global.asax Application_Start:

 var routes = RouteTable.Routes;

 routes.MapRoute(
            "ID Action", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new {controller = "Home", action = "SetID", ID = ""} // Parameter defaults
            );

Upvotes: 3

arnistrong
arnistrong

Reputation: 151

Look in your Global.asax. There you define a default route and a default parameter. Like this:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Upvotes: 0

Related Questions