1

I have the following code below.

public IHttpActionResult Login([FromBody]LoginVM login)
{
    bool isAuthenticated = EmployeeSecurity.Login(login.quad, login.password);
    if (isAuthenticated)
    {
        var response = Request.CreateResponse(HttpStatusCode.OK, "Authorized"); 
        response.Headers.Add("Token", "test");
        response.Headers.Add("TokenExpiry", "testdate");
        response.Headers.Add("Role", "testrol");
        response.Headers.Add("Access-Control-Expose-Headers", "Token,TokenExpiry");
        return Content(HttpStatusCode.OK, response);
    }
    return Content(HttpStatusCode.Unauthorized, "Please check your QUAD or password.");
}

I have the following error

"Error getting value from 'ReadTimeout' on 'System.Web.HttpInputStream'.",

I do have the codes below in global.aspx.

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
                .ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters
                .Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);

Other API endpoints work correctly. What am I doing wrong here?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Aatish Kumar
  • 149
  • 2
  • 15
  • I am not familiar with this API, but from sample code here https://stackoverflow.com/questions/15816049/ you should `return response;` – devio May 15 '18 at 06:58
  • @devio response works but i wanted to return IHttpActionResult. Guess I will stick to response. – Aatish Kumar May 15 '18 at 07:00
  • It may help: https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/action-results – Bruno Quintella May 15 '18 at 07:37
  • 1
    @AatishKumar *why* do you want to return an IHttpActionResult instead of an `IActionResult` like all Web API methods do? After all, your method returns only a status code with a description, not even an actual content – Panagiotis Kanavos May 15 '18 at 08:13

1 Answers1

0

Try this:

public static class HttpActionResultExtentions
{
    public static IHttpActionResult AddHeader(this IHttpActionResult result, string name, IEnumerable<string> values)
        => new HeaderActionResult(result, name, values);
private class HeaderActionResult : IHttpActionResult
    {
        private readonly IHttpActionResult actionResult;

        private readonly Tuple<string, IEnumerable<string>> header;

        public HeaderActionResult(IHttpActionResult actionResult, string headerName, IEnumerable<string> headerValues)
        {
            this.actionResult = actionResult;

            header = Tuple.Create(headerName, headerValues);
        }

        public async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            var response = await actionResult.ExecuteAsync(cancellationToken);

            response.Headers.Add(header.Item1, header.Item2);

            return response;
        }
    }
}

I Get this form https://gist.github.com/testfirstcoder/90e6f09c4f40b36c2dd1

  • Code only answers are *not* considered good asnwers. You should explain how this code answers the original question, if at all. The OP didn't ask how to add headers to the response – Panagiotis Kanavos May 15 '18 at 07:51