1

I could sucefully redirect WebAPI request to another server, can someone help me to do the same with MVC? Its not really a redirect, its a new request to the other server I would like to do same with MVC Controllers, but I have no idea on how to procced.

public class RedirecionarServidorPrincipalHandler : DelegatingHandler
{

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        if (myCondition)
        {
            var path = request.RequestUri.AbsolutePath;
            if (path.Contains("blabla"))
            {
                string url = request.RequestUri.OriginalString.Replace(request.RequestUri.Host, theOtherHost);

                var requisicao = new HttpRequestMessage(request.Method, new Uri(url))
                {
                    Version = request.Version,

                };

                if (request.Method.Method != "GET")
                    requisicao.Content = request.Content;

                foreach (var header in request.Headers)
                    requisicao.Headers.Add(header.Key, header.Value);

                foreach (var propertie in request.Properties)
                    requisicao.Properties.Add(propertie);


                using (var client = new HttpClient())
                {
                    var ret = await client.SendAsync(requisicao, cancellationToken);
                    var novoRetorno = new HttpResponseMessage(ret.StatusCode)
                    {
                        Content = ret.Content,
                        ReasonPhrase = ret.ReasonPhrase,
                        Version = ret.Version,
                        RequestMessage = request
                    };
                    foreach (var header in ret.Headers)
                        novoRetorno.Headers.Add(header.Key, header.Value);

                    return novoRetorno;
                }
            }
        }
        return await base.SendAsync(request, cancellationToken);
    }
}

1 Answers1

0

Well, since I couldn't find a way to do that and wasn't a real need for the MVC controllers, I've changed them to WebApi controller, were just 2 and small ones, that solved my problem, but if anyone knows a way, post here to future visists.