Created
November 28, 2012 08:47
-
-
Save kevinobee/4159952 to your computer and use it in GitHub Desktop.
Multiple GET methods with WebApi
This file contains 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
using System.Net.Http; | |
using System.Web.Http; | |
namespace MvcApplication1.Controllers | |
{ | |
public class GetDataController : ApiController | |
{ | |
public string Get() | |
{ | |
return Request.RequestUri.Host; | |
} | |
public string Get(int id) | |
{ | |
return string.Format("{0} - {1}", Request.RequestUri.Host, id); | |
} | |
public HttpResponseMessage GetScheme() | |
{ | |
return new HttpResponseMessage() { Content = new StringContent(Request.RequestUri.Scheme.ToLower()) }; | |
} | |
} | |
} |
This file contains 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
using System.Net.Http; | |
using System.Web.Http; | |
using System.Web.Http.Routing; | |
namespace MvcApplication1 | |
{ | |
public static class WebApiConfig | |
{ | |
public static void Register(HttpConfiguration config) | |
{ | |
config.Routes.MapHttpRoute( | |
"DefaultApiWithId", | |
"Api/{controller}/{id}", | |
new { id = RouteParameter.Optional }, new { id = @"\d+" }); | |
config.Routes.MapHttpRoute( | |
"DefaultApiWithAction", | |
"Api/{controller}/{action}"); | |
config.Routes.MapHttpRoute( | |
"DefaultApiGet", | |
"Api/{controller}", | |
new { action = "Get" }, | |
new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }); | |
config.Routes.MapHttpRoute( | |
"DefaultApiPost", | |
"Api/{controller}", | |
new { action = "Post" }, | |
new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Routing solution came from http://stackoverflow.com/questions/9499794/single-controller-with-multiple-get-methods-in-asp-net-web-api