I am barely starting out with my first project on the ASP.NET MVC project type and I am creating a Details page where instead of passing the templated (int id), I would like to pass a string instead. But when I am in debug mode, and enter this in the URL, “myString” is null. Why so? Do I have to change anything else somehwere else?
So if I go to the URL and enter this:
http://localhost:2345/Bank/EmployeeDetails/3d34xyz
public ActionResult EmployeeDetails(string myString) // myString is null
{
return View();
}
In you
Global.asax.csfile, you will have the following route mapped by default:That means that an url like
http://localhost:2345/Bank/EmployeeDetails/3d34xyzwill go to theBankcontroller, theEmployeeDetailsaction and pass the value3d34xyzinto a parameter namedid. It is perfectly alright to pass a string, but in order to make it work you have two options:1) Rename the variable to
idin your action method.2) Add another route that matches whatever name you want for your string. Make sure to make it more specific than the default route, and to place it before the default route in the
Global.asax.csfile.This will pass a default value of
nulltomyStringif no value is passed in the url, but with the url you specified you will pass the value3d34xyz.