Created
April 4, 2012 02:52
-
-
Save shawnmclean/2297415 to your computer and use it in GitHub Desktop.
Absolute Url in Asp.net webapi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Your ValuesController.cs | |
//Version 1 | |
string uri = Url.Route("Default", new { controller="Home", action="MyAction", id = 1 }); | |
string absoluteUrl = new Uri(Request.RequestUri, uri).AbsoluteUri; | |
//http://localhost/Home/MyAction/1 | |
//Version 2 | |
string uri = Url.Route("DefaultApi", new { id = 1 }); | |
string absoluteUrl = new Uri(Request.RequestUri, uri).AbsoluteUri; | |
//http://localhost/api/Values/1 | |
//Your Global.asax | |
routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{id}/{action}", | |
defaults: new {action = "Index", id = RouteParameter.Optional } | |
); | |
routes.MapRoute( | |
name: "Default", | |
url: "{controller}/{action}/{id}", | |
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment