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

Hello,

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:
www.mydomain.com/id=myID

Then:
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
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) 
  routes.MapRoute( 
  "Default",               
  "{controller}/{action}/{id}",        
  new { controller = "Home", action = "Index", id = "" }   
  ); 
    
public ActionResult Index(int? id) 
  if (id == 123) 
  
  // do my logic here
  
    return View();
  
 
  else
  
  return View(); 
  
}

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

www.mydomain.com/Home/Index/123

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.