Is it possible to add custom headers to asp.net MVC 4 Web API?
1 Answer
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
});
1 Comment
dave heywood
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...