Last active
June 22, 2018 06:38
-
-
Save MinChanSike/70d1078f745ab45f45a3d0e076b0bd88 to your computer and use it in GitHub Desktop.
ASP.Net WebApi Custom Route
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
| //Web Api Config | |
| private void ConfigureWebApi(IAppBuilder app) { | |
| var config = new HttpConfiguration(); | |
| config.MapHttpAttributeRoutes(); | |
| config.Routes.MapHttpRoute( | |
| name: "DefaultApi", | |
| routeTemplate: "api/{controller}/{id}", | |
| defaults: new { id = RouteParameter.Optional } | |
| ); | |
| config.UserCoreServices(); | |
| app.UseWebApi(config); | |
| } | |
| //Controller | |
| public class ReportController : ApiController { | |
| [HttpGet] | |
| [Route("api/report/one")] | |
| ///http://localhost:8000/api/report/one?id=1 | |
| public string GetOne(string id) { | |
| return $"GetOne => param {id}"; | |
| } | |
| [HttpGet] | |
| [Route("api/report/two")] | |
| ///http://localhost:8000/api/report/one?id=2 | |
| public string GetTwo(string id) { | |
| return $"GetTwo => param {id}"; | |
| } | |
| [HttpGet] | |
| [Route("api/report/test1")] | |
| ///http://localhost:8000/api/report/test1?AreaId=01&FromDate=89987878 | |
| public string Test1(string AreaId, string FromDate) { | |
| return $"Test1 => Id: {AreaId}, Name: {FromDate}"; | |
| } | |
| [HttpGet] | |
| [Route("api/report/test2")] | |
| ///http://localhost:8000/api/report/test1?AreaId=02&FromDate=89987878 | |
| public string Test2(string AreaId, string FromDate) { | |
| return $"Test2 => Id: {AreaId}, Name: {FromDate}"; | |
| } | |
| [HttpGet] | |
| [Route("api/report/object")] | |
| ///http://localhost:8000/api/report/object?AreaId=01&Name=AreaName | |
| public string GetObject([FromUri]AreaInfo info) { | |
| return $"GetObject => Id:{info.AreaId}, Name:{info.Name}"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment