WebApi2 IHttpActionResult strongly typed return values

后端 未结 4 1427
借酒劲吻你
借酒劲吻你 2021-01-18 16:10

It is possible and not ideal to do this: (a vastly simplified example!)

[Serializable]
public class MyRecord
{
    public string key {get; set;}
    public          


        
4条回答
  •  迷失自我
    2021-01-18 16:34

    First of all, it is a good practice to return IHttpActionResult, indicating the corresponding http status. Something like:

    public async Task Get(string SomeKey)
    {
        if(ExistsInDB(SomeKey))
            return Ok(SomeRecordFromDB(SomeKey));
    
        return NotFound();
    }
    

    But if you really want to have a strongly typed api, you can do something like:

     public async Task Get()
     {
         return new StronglyTypeResponse();
     }
    

    Or, create a typed response, and you'll keep the http status code, etc in your response:

    public class StronglyTypeResponse
    {
    
    }
    
    public class StronglyTypedResult : IHttpActionResult
    {
        HttpConfiguration _configuration;
        T _content;
        HttpStatusCode _statusCode;
        HttpRequestMessage _request;
    
        public StronglyTypedResult(T content, HttpStatusCode statusCode, HttpRequestMessage request, HttpConfiguration configuration)
        {
            _content = content;
            _request = request;
            _configuration = configuration;
            _statusCode = statusCode;
        }
    
        public Task ExecuteAsync(CancellationToken cancellationToken)
        {
            var response = new HttpResponseMessage(_statusCode)
            {
                Content = new ObjectContent(_content, _configuration.Formatters.JsonFormatter),
                RequestMessage = _request,
                ReasonPhrase = "some phrase"
            };
    
            return Task.FromResult(response);
        }
    }
    

    And then you can create your method:

    public async Task> Get()
    {
        return new StronglyTypedResult(new StronglyTypeResponse(), HttpStatusCode.OK, Request, Configuration);
    }
    

    Creating your own custom generic IHttpActionResult

    It's basically the same:

    public class IHttpActionResult : System.Web.Http.IHttpActionResult
    {
        HttpConfiguration _configuration;
        T _content;
        HttpStatusCode _statusCode;
        HttpRequestMessage _request;
    
        public IHttpActionResult(T content, HttpStatusCode statusCode, HttpRequestMessage request, HttpConfiguration configuration)
        {
            _content = content;
            _request = request;
            _configuration = configuration;
            _statusCode = statusCode;
        }
    
        public Task ExecuteAsync(CancellationToken cancellationToken)
        {
            var response = new HttpResponseMessage(_statusCode)
            {
                Content = new ObjectContent(_content, _configuration.Formatters.JsonFormatter),
                RequestMessage = _request,
                ReasonPhrase = "some phrase"
            };
    
            return Task.FromResult(response);
        }
    }
    

    And now you can return the class you want with a typed IHttpActionResult:

    public async Task> Get()
    {
        var yourclass = new YourClass();
    
        return new IHttpActionResult(yourclass, HttpStatusCode.OK, Request, Configuration);
    }
    

提交回复
热议问题