Skip to main content
A newer version of this page is available. .
All docs
V21.2

Create Custom Endpoints

You can create custom endpoints for the Web API service.

  1. Right-click the project that contains a Web API service and select Add -> New Item in the context menu. Choose the API Controller – Empty template in the invoked window.

    Add API Controller

  2. Add custom endpoint methods to the new Controller (the Get, Post, Put, and Delete methods in the example below).

  3. If you wish to use the Web API authentication, decorate the new Controller with AuthorizeAttribute. See the following topic for more information on how to configure the authentication: Web API Authentication (CTP).

The Controller’s code:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace MySolution.WebApi {
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class CustomEndpoint1Controller : ControllerBase {
        [HttpGet]
        public IEnumerable<string> Get() {
            return new string[] { "value1", "value2" };
        }

        [HttpGet("{id}")]
        public string Get(int id) {
            return "value";
        }

        [HttpPost]
        public void Post([FromBody] string value) {
        }

        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value) {
        }

        [HttpDelete("{id}")]
        public void Delete(int id) {
        }
    }
}

The result in the Swagger UI:

Web API Custom Endpoint

See Also