WebApi uses custom routing and BaseController

Create a new BaseController, inherited from ApiController

public abstract class BaseController<TModel> : ApiController
        where TModel : class
    {
        [HttpPost]
        [Route("")]
        public abstract TModel Insert([FromBody] TModel model);

        [HttpPut]
        [Route("")]
        public abstract TModel Update([FromBody] TModel model);

        [HttpDelete]
        [Route("{autoId:int}")]
        public abstract bool Delete([FromUri] int autoId);

        [HttpGet]
        [Route("")]
        public abstract IEnumerable<TModel> GetAll();

        [HttpGet]
        [Route("{autoId:int}")]
        public abstract TModel GetById([FromUri] int autoId);

        [HttpGet]
        [Route("GetPage")]
        public abstract PageResult<TModel> GetPage([FromUri] PageOption option);
    }

    public class PageResult<TModel>
        where TModel : class
    {
        public int Count { get; set; }

        public IEnumerable<TModel> Items { get; set; }
    }

    public class PageOption
    {
        public int CurrentPage { get; set; }

        public int Top { get; set; }
    }

Create a new UserController, inherit BaseController

    [RoutePrefix("api/users")]
    public class UserController : BaseController<User>
    {
        public override bool Delete([FromUri] int autoId)
        {
            return true;
        }

        public override IEnumerable<User> GetAll()
        {
            return new List<User>();
        }

        public override User GetById([FromUri] int autoId)
        {
            return new User
            {
                AutoId = autoId,
                Name = "GetById"
            };
        }

        public override PageResult<User> GetPage([FromUri] PageOption option)
        {
            return new PageResult<User>
            {
                Count = option.Top,
                Items = new List<User>()
            };
        }

        public override User Insert([FromBody] User model)
        {
            return model;
        }

        public override User Update([FromBody] User model)
        {
            return model;
        }
    }

    public class User
    {
        public int AutoId { get; set; }

        public string Name { get; set; }
    }

In the above code, two features are used, one is RoutePrefix, the other is Route. The code written in this way can pass the
http://xxx/api/users The address can be accessed after waiting, but when the project is running, an error is always reported because it needs to be configured in WebApiConfig

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routing
            config.MapHttpAttributeRoutes(new CustomDirectRouteProvider());

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

    public class CustomDirectRouteProvider : DefaultDirectRouteProvider
    {
        protected override IReadOnlyList<IDirectRouteFactory> GetActionRouteFactories(HttpActionDescriptor actionDescriptor)
        {
            return actionDescriptor.GetCustomAttributes<IDirectRouteFactory>(true);
        }
    }

It's not hard to see from the above code,
config.MapHttpAttributeRoutes(new CustomDirectRouteProvider());
A custom route is used. After configuration, you can access GET and POST methods. What about PUT and DELETE?
Because IIS does not allow access to PUT and DELETE by default, you need to configure it again in Web.config

<system.webServer>
    <handlers>
        <remove name="WebDAV" />
    </handlers>
    <modules>
        <remove name="WebDAVModule"/>
    </modules>
</system.webServer>

As shown in the above code, remove WebDAV and WebDAVModule from the two nodes of handlers and modules

Keywords: IIS

Added by double on Mon, 30 Mar 2020 18:15:02 +0300