2

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

1 Answer 1

5

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
    });
Sign up to request clarification or add additional context in comments.

1 Comment

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...

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.