2

In my api controller action method. I am using Content Negotation for Get Request. the code is :

IContentNegotiator negotiator = this.Configuration.Services.GetContentNegotiator();
ContentNegotiationResult result = negotiator.Negotiate(typeof(OfficeDetailsDto), this.Request, this.Configuration.Formatters);
if (result == null)
{
    var responseErr = new HttpResponseMessage(HttpStatusCode.NotAcceptable);
    throw new HttpResponseException(responseErr);
}

But now I want create a BaseAPIController controller by inheriting ApiController and want to override a base class's method to have the above code so that I don't have to write this code again and again in all my controller classes. It will also make my controller action method thin.

Can anyone give me any suggestion or sample please.

1 Answer 1

2

ApiController already provides the ability to handle content negotiation out of the box, but you could create your own generic method(s) that you can call from inherited controllers if you really want to customize the content negotiation process.

Your BaseAPIController

public abstract class BaseAPIController : ApiController {    
    protected virtual HttpResponseMessage NegotiatedContent<T>(HttpStatusCode statusCode, T content) {
        var type = typeof(T);
        var request = this.Request;
        var formatters = this.Configuration.Formatters;
        var negotiator = this.Configuration.Services.GetContentNegotiator();

        var result = negotiator.Negotiate(type, request, formatters );
        if (result == null) {
            var response = new HttpResponseMessage(HttpStatusCode.NotAcceptable);
            throw new HttpResponseException(response));
        }

        return new HttpResponseMessage() {
            StatusCode = statusCode,
            Content = new ObjectContent<T>(
                content,                    // What we are serializing 
                result.Formatter,           // The media formatter
                result.MediaType.MediaType  // The MIME type
            )
        };
    }    
}

This code is equivalent to the what ApiController provides automatically.

A potential OfficeDetailsController with a much thinner action method

public class OfficeDetailsController : BaseAPIController {    
    public HttpResponseMessage GetOfficeDetails(int id) {
        var item = new OfficeDetailsDto() { Id = id, Name = "Gizmo"};
        // what ever else needs to be done to the item
        // ...    
        return NegotiatedContent(HttpStatusCode.Ok, item);
    }
}

Here is an example of doing the same thing using ApiController defaults.

public class OfficeDetailsController : ApiController {    
    public IHttpActionResult GetOfficeDetails(int id) {
        var item = new OfficeDetailsDto() { Id = id, Name = "Gizmo"};
        // what ever else needs to be done to the item
        // ...    
        return Ok(item);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.