4

Here it is our middleware's code snippet

public Task InvokeAsync(HttpContext context)
{
    if(!found)
    {
        context.Response.StatusCode = StatusCodes.Status401Unauthorized;
        return context.Response.WriteAsync("Something wrong");
    }
    return _next(context);
}

The problem is that while the client app receives error code 401 which is fine but "Something wrong" string is not found in the response's body. What are we missing here?

Peter B
  • 22,460
  • 5
  • 32
  • 69
Arash
  • 3,628
  • 5
  • 46
  • 70
  • 1
    Did another middleware clear the response and change the output? – Eilon Jul 16 '18 at 20:12
  • 2
    I fail to reproduce your issue with implementing this middleware. I suggest you try to move this middlware to the `first line` in `Configure` in `Startup.cs`. Could you share us a demo to reproduce your issue? – Edward Jul 17 '18 at 01:52

4 Answers4

4

An interesting solution that worked for us:

The following statement: return context.Response.WriteAsync("Something wrong");

was replaced by this one:

return context.Response.Body.WriteAsync("Something wrong").AsTask();

This change made the response be populated properly. Although, I'm not sure yet why this way of response body population would work but not the initial method.

Arash
  • 3,628
  • 5
  • 46
  • 70
3

You should also set content-type. Something like this

    context.Response.StatusCode = StatusCodes.Status401Unauthorized;
    context.Response.ContentType = "text/plain";
    return context.Response.WriteAsync("Something wrong");
Torben Nielsen
  • 663
  • 1
  • 8
  • 21
2

The static and asynchronous method HttpResponseWritingExtensions.WriteAsync is currently the preferred way of reaching this goal.

Currently, you can find it in the assembly Assembly Microsoft.AspNetCore.Http.Abstractions.

using Microsoft.AspNetCore.Http;

public Task InvokeAsync(HttpContext context)
{
    if (found == false)
    {
        context.Response.StatusCode = StatusCodes.Status401Unauthorized;
        await HttpResponseWritingExtensions.WriteAsync(context.Response, "Something wrong");
    }
    return _next(context);
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
  • Please [don't post identical answers to multiple questions](https://meta.stackexchange.com/q/104227). Instead, tailor the answer to the question asked. If the questions are exact duplicates of each other, please vote/flag to close instead. – Samuel Liew Feb 03 '19 at 00:08
  • I went ahead and marked your flag as helpful and undeleted this answer, but Samuel's point still stands. You recently posted (and then self-deleted) a number of *nearly* identical answers. It's great that you learned something and want to help, but if multiple questions have exactly the same answer, then *extremely* high odds that they are duplicates. We have better ways of dealing with duplicates than posting the same or similar answer to each one: flagging them as duplicates helps to get all the answers in one place and helps to ensure quality. Please do that in the future. – Cody Gray - on strike Feb 03 '19 at 02:36
  • 1
    Also, I've edited a couple of your most recent answers. The use of the bold **"The correct answer"** and the emojii are very off-putting here. Obviously you think it's the correct answer, or you wouldn't have posted. And thumbs-up emojii have no place in a professional setting like this website. – Cody Gray - on strike Feb 03 '19 at 02:37
0

you should use wait at the last of the Async function in this method:

HttpContext.Response.WriteAsync("Something wrong").Wait();

Reference: What's the difference between Task.Start/Wait and Async/Await?

  • 4
    you mean `await HttpContext.Response.WriteAsync("Something wrong");` – Tsahi Asher Jan 09 '19 at 08:10
  • 1
    You should use `await`, to let the request thread handle other requests while the call is executing, instead of blocking the thread until it completes, just as it says in your link. This will improve overall server performance. – Tsahi Asher Jan 09 '19 at 09:08
  • i agree with you, but in this situation, we don't have any choice. because C# doesn't handle this method and sometime it breaks and crashes. – Hamed Bagheri Jan 09 '19 at 10:57