James
James

Reputation: 6499

ASP.Net MVC Route not passing parameter to controller

I'm trying to learn MVC and having a rough start.

routes.MapRoute(
       "Test",
       "Test/{stringInput}",
       new { controller = "Test", action = "TestMethod", stringInput = "" }
);

doesn't pass stringInput to the method TestMethod in the controller. It comes over null.

Not sure what I'm missing, it seems very simple and straightforward. This route was placed above the default.

Upvotes: 2

Views: 2447

Answers (3)

Damien
Damien

Reputation: 14057

Override the Execute method of your controller and then put a breakpoint in it so you can see the request context. One of the properties is the key/value pair being passed in. Make sure the key for stringInput has the correct value.

Upvotes: 3

Josh Close
Josh Close

Reputation: 23383

Make sure your controller is setup properly. It should be in folder

Controllers/TestController.cs

and inside the controller should be

public ActionResult TestMethod( string stringInput )
{
    return View();
}

It uses conventions, so the naming you setup in your route needs to match the files, methods, and parameters of the controller.

The url to get to this should be

/Test/TestMethod/MyStringInput

and "MyStringInput" would be the value of the variable stringInput.

Upvotes: 1

Alan Jackson
Alan Jackson

Reputation: 6511

Are you sure that route is the one being used? Try moving it to the top of your list of routes to make sure.

Upvotes: 0

Related Questions