user1135534
user1135534

Reputation: 575

Adding Custom Header to asp.net MVC 4 Web API

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

Upvotes: 2

Views: 1728

Answers (1)

Kevin Junghans
Kevin Junghans

Reputation: 17540

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
    });

Upvotes: 5

Related Questions