WebApi使用自定义路由与BaseController

新建一个BaseController,继承自ApiController

public abstract class BaseController : ApiController
    where T : class, IApiModel
{
    [HttpPost]
    [Route("")]
    public abstract Task Insert([FromBody] T model);

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

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

    [HttpGet]
    [Route("")]
    public abstract Task> GetAll();

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

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

public class PageResult
    where T : class
{
    public int Count { get; set; }

    public IEnumerable Items { get; set; }
}

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

    public int Top { get; set; }
}

新建UserController,继承BaseController

[RoutePrefix("api/users")]
public class UserController : BaseController
{
    public override Task Delete([FromUri] int autoId)
    {
        throw new NotImplementedException();
    }

    public override Task> GetAll()
    {
        throw new NotImplementedException();
    }

    public override Task GetById([FromUri] int autoId)
    {
        throw new NotImplementedException();
    }

    public override Task> GetPage([FromUri] PageOption option)
    {
        throw new NotImplementedException();
    }

    public override Task Insert([FromBody] User model)
    {
        throw new NotImplementedException();
    }

    public override Task Update([FromBody] User model)
    {
        throw new NotImplementedException();
    }
}

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

    public string Name { get; set; }
}

在以上的代码使用了2个特性,一个是RoutePrefix,另一个是Route,按理说这样写出来的代码就可以通过
http://xxx/api/users等之类的地址就可以访问了,但把项目运行起来,发现一直报错是,原因是需要在WebApiConfig中配置

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API 配置和服务

        // Web API 路由
        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 GetActionRouteFactories(HttpActionDescriptor actionDescriptor)
    {
        return actionDescriptor.GetCustomAttributes(true);
    }
}

通过上面代码不难看出,
config.MapHttpAttributeRoutes(new CustomDirectRouteProvider());
使用了一个自定义的路由,配置完以后,就可访问GET与POST方法了,那PUT和DELETE呢?
因为IIS默认是不允许访问PUT和DELETE,所以,就需要在Web.config中再配置一下

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

如上面代码,上handlers与modules的2个节点中加入移除WebDAV和WebDAVModule就可以了

你可能感兴趣的:(.Net/C#)