我需要创建一个重定向器,将用户重定向到外部域,同时保留查询参数和一个附加参数。
例如当用户访问时
https://contoso.com/redirect?docId=123
,它将用户重定向到
https://contoso-v2.com/home?docId=123&token=xxxxxxx
一旦用户访问 https://contoso.com/redirect?docId=123
,此终结点将处理信息(来自查询参数)并生成需要附加到目标 URL 中的 token .
Azure 中最有效、最好的方法是什么?编写一个简单的 Azure Web App 还是有更好的方法?
最佳答案
您可以使用Azure Function与 HttpTrigger Binding 。与consumption plan成本将是最小的(1 million invocations are free in pay-as-you-go plan)。
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
var uri = req.RequestUri;
var updatedUri = ReplaceHostInUri(uri, "contoso-v2.com");
//return req.CreateResponse(HttpStatusCode.OK, "Original: " + uri + " Updated: " + updatedUri);
return req.CreateResponse(HttpStatusCode.Found, updatedUri);
}
private static string ReplaceHostInUri(Uri uri, string newHostName) {
var builder = new UriBuilder(uri);
builder.Host = newHostName;
//Do more trasformations e.g. modify path, add more query string vars
return builder.Uri.ToString();
}
关于azure - Azure 中的重定向服务 (302),最好的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55441112/