Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 20th, 2012  |  syntax: None  |  size: 0.74 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Your ValuesController.cs
  2.  
  3. //Version 1
  4. string uri = Url.Route("Default", new { controller="Home", action="MyAction", id = 1 });
  5. string absoluteUrl = new Uri(Request.RequestUri, uri).AbsoluteUri;
  6. //http://localhost/Home/MyAction/1
  7.  
  8. //Version 2
  9. string uri = Url.Route("DefaultApi", new { id = 1 });
  10. string absoluteUrl = new Uri(Request.RequestUri, uri).AbsoluteUri;
  11. //http://localhost/api/Values/1
  12.  
  13. //Your Global.asax
  14.  
  15. routes.MapHttpRoute(
  16.     name: "DefaultApi",
  17.     routeTemplate: "api/{controller}/{id}/{action}",
  18.     defaults: new {action = "Index", id = RouteParameter.Optional }
  19. );
  20.  
  21. routes.MapRoute(
  22.     name: "Default",
  23.     url: "{controller}/{action}/{id}",
  24.     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
  25. );