Routing with multiple Get methods in ASP.NET Web API

前端 未结 10 795
闹比i
闹比i 2020-11-27 10:13

I am using Web Api with ASP.NET MVC, and I am very new to it. I have gone through some demo on asp.net website and I am trying to do the following.

I have 4 get meth

10条回答
  •  执念已碎
    2020-11-27 10:52

      // this piece of code in the WebApiConfig.cs file or your custom bootstrap application class
      // define two types of routes 1. DefaultActionApi  and 2. DefaultApi as below
    
       config.Routes.MapHttpRoute("DefaultActionApi", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });
       config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { action = "Default", id = RouteParameter.Optional });
    
      // decorate the controller action method with [ActionName("Default")] which need to invoked with below url
      // http://localhost:XXXXX/api/Demo/ -- will invoke the Get method of Demo controller
      // http://localhost:XXXXX/api/Demo/GetAll -- will invoke the GetAll method of Demo controller
      // http://localhost:XXXXX/api/Demo/GetById -- will invoke the GetById method of Demo controller
      // http://localhost:57870/api/Demo/CustomGetDetails -- will invoke the CustomGetDetails method of Demo controller
      // http://localhost:57870/api/Demo/DemoGet -- will invoke the DemoGet method of Demo controller
    
    
     public class DemoController : ApiController
     {
        // Mark the method with ActionName  attribute (defined in MapRoutes) 
        [ActionName("Default")]
        public HttpResponseMessage Get()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "Get Method");
        }
    
        public HttpResponseMessage GetAll()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "GetAll Method");
        }
    
        public HttpResponseMessage GetById()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "Getby Id Method");
        }
    
        //Custom Method name
        [HttpGet]
        public HttpResponseMessage DemoGet()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "DemoGet Method");
        }
    
        //Custom Method name
        [HttpGet]
        public HttpResponseMessage CustomGetDetails()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "CustomGetDetails Method");
        }
    }
    

提交回复
热议问题