Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

c# - How to write custom actionResult in asp.net core

In webApi2 i could write a custom ActionResult by inheriting IHttpActionResult.

Sample :

public class MenivaActionResult : IHttpActionResult
    {
        private readonly HttpRequestMessage _request;
        private readonly ServiceResponseBase _response;

        public MenivaActionResult(HttpRequestMessage request, ServiceResponseBase response)
        {
            _request = request;
            _response = response;
        }


        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken token)
        {

            HttpResponseMessage httpresponseMessage = _response.Exception != null
                ? _request.CreateErrorResponse((HttpStatusCode)_response.HttpCode,
                    _response.Exception.Message+":"+_response.Exception.HelpCode)
                : _request.CreateResponse((HttpStatusCode)_response.HttpCode, _response.Data);
            return Task.FromResult(httpresponseMessage);
        }
    }

The ServiceResponseBase class object holds all the exception and data from service layer..

How i can port this code to asp.net core. I tried but i dont know how create a response message from HttpRequestMessage object in .net core. The IActionResult only have Task ExecuteResultAsync(ActionContext context) method. how i can modify the response from this method

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is an example:

public class TestResult
{
    public Exception Exception { get; set; }
    public object Data { get; set; }
}

public class TestActionResult : IActionResult
{
    private readonly TestResult _result;

    public TestActionResult(TestResult result)
    {
        _result = result;
    }

    public async Task ExecuteResultAsync(ActionContext context)
    {
        var objectResult = new ObjectResult(_result.Exception ?? _result.Data)
        {
            StatusCode = _result.Exception != null
                ? StatusCodes.Status500InternalServerError
                : StatusCodes.Status200OK
        };

        await objectResult.ExecuteResultAsync(context);
    }
}

ObjectResult is the type your results are converted if you return a non-IActionResult from an action. It will do content negotiation for you.

You could also inherit from ObjectResult and setup the status code and data to be written in the constructor.

More on content negotiation in ASP.NET Core: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/formatting#content-negotiation


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...