Is it possible to add custom headers to asp.net MVC 4 Web API?

share|improve this question
feedback

1 Answer

up vote 1 down vote accepted

I assume you are talking about HTTP headers. And I am not sure if you want to customize the response or the request. You can do both. Here is a blog post that shows how to add a custom header in the response using and ActionFilterAttribute. If you are using JQuery ajax to send a request to the Web API you can use the beforeSend event to add a custom header. Here is an example of using it for basic authentication.

    $.ajax({
        url: _url,
        data: _data,
        type: _type,
        beforeSend: function (xhr) {
            var up = username + ":" + password;
            xhr.setRequestHeader("Authorization", "Basic " + up);
        },
        success: _successFunc,
        error: _errorFunc
    });
share|improve this answer
thanks, i was talking about HTTP headers – user1135534 Oct 4 '12 at 0:14
brilliantly simple response - after a few hours going around in circles, this made my afternoon - especially the link detailing the "Same Origin Policy" and "Cross-Origin Resource Sharing" stuff I've been dancing around all day..., this response should be on 10x answers on SO... – dave -pointbypoint Dec 10 '12 at 15:38
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.