aspnet / AspNetWebStack

ASP.NET MVC 5.x, Web API 2.x, and Web Pages 3.x (not ASP.NET Core)
Other
858 stars 354 forks source link

Generic support for IHttpActionResult #353

Closed sahin52 closed 2 years ago

sahin52 commented 2 years ago

IHttpActionResult interface currently does not support generic method, but sometimes we have to use it and specify the return type, in order to prevent any errors and understand the return type easily.

Here is an example:

using System.Web.Http;

namespace temp.Controllers
{
    public class TempController : ApiController
    {
        [Route("http-action")]
        [HttpGet]
       public IHttpActionResult GetHttpAction()
        {
            return Ok("Success");
            return InternalServerError();
            return BadRequest();
            return Redirect("some-where");
            return Ok(3);
            return Ok(new { Test=3 });
        }
        [Route("string")]
        [HttpGet]
        public string GetString()
        {
            return "Success";
        }
        [Route("generic")]
        [HttpGet]
        public IHttpActionResult<string> GetGeneric() // gives error :(
        {
            return Ok("Success");
        }
    }
}

The first line function is IHttpActionResult but we can give any return type. It doesn't give any error. By this method, we can return any Http result like Redirect, or BadRequest or Ok etc, but we can't know what Ok will return. Most of our api endpoints return an object, rather than other Http results. Second function specifies the return type but we can't return the other Http results unfortunately. Third function sprecifies return type and can return Http results but is not supported :(

Is there a problem with adding this? Can we add this functionality?

mkArtakMSFT commented 2 years ago

Thanks for contacting us. We're not making any improvements in this area any more as this project is in maintenance mode. Only critical blocking issue with wide impact and security issues are considered.

sahin52 commented 2 years ago

For other people struggling for this: you can still return any object and create BadRequest in some cases or something with the help of HttpResponseException. Just look here: https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/action-results#other-return-types