Home > Software engineering >  How to add more custom methods in HttpRequestMessageExtensions class
How to add more custom methods in HttpRequestMessageExtensions class

Time:05-07

return Request.CreateResponse(HttpStatusCode.BadRequest, new mymethodresponse
                {
                    ID= Idval,
                    Created= false
                }

Here how to create more overloads for createresponse method

CodePudding user response:

Below is a work demo, you can refer to it.

1.Create a HttpRequestMessageExtensions class

public static class HttpRequestMessageExtensions
    {
        public static HttpResponseMessage CreateResponse(this HttpRequestMessage request, HttpStatusCode statusCode, string TestParas)
        {
            return new HttpResponseMessage
            {
                StatusCode = statusCode,
                RequestMessage = request
            };
        }
     }

2.In controller like below

public class OverloadController : Controller
    {
        public HttpResponseMessage Call(HttpRequestMessage RequestMessage)
        {
            return RequestMessage.CreateResponse();//do your staff...

        }
     
    }

Result: enter image description here

  • Related