返回IHttpActionResult

时间:2018-05-15 06:53:32

标签: c# asp.net-mvc asp.net-web-api asp.net-web-api2

我在下面有以下代码。

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.");
}

我有以下错误

  

"从' ReadTimeout'中获取值时出错上   ' System.Web.HttpInputStream'",

我在global.aspx中有以下代码。

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

其他API端点正常工作。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

试试这个:

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;
        }
    }
}

我收到此表单https://gist.github.com/testfirstcoder/90e6f09c4f40b36c2dd1