“ExecuteResultasync ActionContext Statuscode” Réponses codées

ExecuteResultasync ActionContext Statuscode

public IActionResult IWillNotBeCalled()
{
    return new UnauthorizedWithMessageResult("MY SQL ERROR");            
}

public class UnauthorizedWithMessageResult : IActionResult
{
    private readonly string _message;

    public UnauthorizedWithMessageResult(string message)
    {
        _message = message;
    }

    public async Task ExecuteResultAsync(ActionContext context)
    {
        // you need to do this before setting the body content
        context.HttpContext.Response.StatusCode = 403;

        var myByteArray = Encoding.UTF8.GetBytes(_message);
        await context.HttpContext.Response.Body.WriteAsync(myByteArray, 0, myByteArray.Length);
        await context.HttpContext.Response.Body.FlushAsync();
    }
}
DreamCoder

ExecuteResultasync ActionContext Statuscode

return new ObjectResult("The message") { 
    StatusCode = (int?) HttpStatusCode.Unauthorized 
};
DreamCoder

Réponses similaires à “ExecuteResultasync ActionContext Statuscode”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code