How to add custom methods to ASP.NET WebAPI controller?

后端 未结 4 1412
悲&欢浪女
悲&欢浪女 2020-12-14 01:52

In ASP.NET MVC WebAPI project by default we have created following controller

 public class ValuesController : ApiController
    {
        /         


        
4条回答
  •  情深已故
    2020-12-14 02:03

    I am not sure I follow as you have GET and POST right there in your code, but in any case you have other options:

    Option 1

    First, you can configure your custom Routes in the App_Start folder in the WebApiConfig.cs file. Here is what I normally use:

        // GET /api/{resource}/{action}
        config.Routes.MapHttpRoute(
            name: "Web API RPC",
            routeTemplate: "{controller}/{action}",
            defaults: new { },
            constraints: new { action = @"[A-Za-z]+", httpMethod = new HttpMethodConstraint("GET") }
            );
        
        // GET|PUT|DELETE /api/{resource}/{id}/{code}
        config.Routes.MapHttpRoute(
            name: "Web API Resource",
            routeTemplate: "{controller}/{id}/{code}",
            defaults: new { code = RouteParameter.Optional },
            constraints: new { id = @"\d+" }
            );
    
        // GET /api/{resource}
        config.Routes.MapHttpRoute(
            name: "Web API Get All",
            routeTemplate: "{controller}",
            defaults: new { action = "Get" },
            constraints: new { httpMethod = new HttpMethodConstraint("GET") }
            );
    
        // PUT /api/{resource}
        config.Routes.MapHttpRoute(
            name: "Web API Update",
            routeTemplate: "{controller}",
            defaults: new { action = "Put" },
            constraints: new { httpMethod = new HttpMethodConstraint("PUT") }
            );
    
        // POST /api/{resource}
        config.Routes.MapHttpRoute(
            name: "Web API Post",
            routeTemplate: "{controller}",
            defaults: new { action = "Post" },
            constraints: new { httpMethod = new HttpMethodConstraint("POST") }
            );
    
        // POST /api/{resource}/{action}
        config.Routes.MapHttpRoute(
            name: "Web API RPC Post",
            routeTemplate: "{controller}/{action}",
            defaults: new { },
            constraints: new { action = @"[A-Za-z]+", httpMethod = new HttpMethodConstraint("POST") }
            );
    

    I use a combination of RESTful endpoints as well as RPC endpoints. For some purists, this is grounds for a holy war. For me, I use a combination of the two because it is a powerful combination and I can't find any sane reason not to.

    Option 2

    As the others have pointed out and as I myself am doing more of these days, use attribute routing:

        [HttpGet]
        [GET("SomeController/SomeUrlSegment/{someParameter}")]
        public int SomeUrlSegment(string someParameter)
        {
            //do stuff
        }
    

    I needed a NuGet package for attribute routing to make this work (just search NuGet for "Attribute Routing"), but I think that MVC 5/WebAPI 2 has it natively.

    Hope this helps.

提交回复
热议问题