ASP.NET - reading a URL in an MVC 4 app - Asked By John Smith on 26-Sep-12 06:27 AM


I am looking to be able to read a URL in an MVC 4 app, and based on parameters in the URL populate text boxes in my app.

So if my URL is:

I want to populate a text box and a label with myID. (Or make a call to a DB. retrieve values based on that parameter, and populate my text box and label with values returned by that parameter.)

I also need to know what the correct URL format will be for constructing a URL that can pass the parameter and will pull up the proper view, as I recognize that the above URL might not even be valid for an MVC app.

TIA for any and all help.

John Smith replied to John Smith on 26-Sep-12 02:26 PM
Figured it out. Put the following into my HomeController.cs file.

public static void RegisterRoutes(RouteCollection routes) 
  new { controller = "Home", action = "Index", id = "" }   
public ActionResult Index(int? id) 
  if (id == 123) 
  // do my logic here
    return View();
  return View(); 

In order for id to be equal to 123 the following is my URL

And being able to get id to be equal to 123 in this example allows me to do what I need to be able to do. Hope this is helpful to someone else.