[英]asp.net response.redirect hitting url, but not changing browser page
[英]ASP.NET: How to redirect without changing URL in the Browser?
我是在 ASP.NET 中创建服务器项目的团队的一员,该项目为登录的成员提供一系列服务。 我们将服务器分成组件,例如用于身份验证。 组件示例见下表。 通过这种方式,它有助于在测试和其他好处方面管理项目。
+-----------------+-------------------+
| Component | Address |
+-----------------+-------------------+
| Authentication | 192.168.0.10:5001 |
| User Management | 192.168.0.10:5002 |
| Search Engine | 192.168.0.10:5003 |
| System Settings | 192.168.0.10:5004 |
+-----------------+-------------------+
我们面临的问题是我们只想在浏览器中使用一个 url,如下表所示。 我们已经将子目录与其各自的组件相匹配。 例如, /login
应该重定向到Authentication
组件。 如果用户导航到搜索页面并输入了搜索词,它应该从www.mysite.com/search?value=test
重定向到192.168.0.10:5003?value=test
。
+-------------------------+-------------------+
| ULR | Redirect |
+-------------------------+-------------------+
| www.mysite.com/ | 192.168.0.10:5000 |
| www.mysite.com/login | 192.168.0.10:5001 |
| www.mysite.com/users | 192.168.0.10:5002 |
| www.mysite.com/search | 192.168.0.10:5003 |
| www.mysite.com/settings | 192.168.0.10:5004 |
+-------------------------+-------------------+
How to program a separate project as gateway in C# ASP.NET to intercept and receive any url path without controllers and redirect to the component without changing the URL in the browser?
您可以使用中间件来处理您的请求:
该中间件获取请求并将其发送到其他应用程序并获取该响应并转发给第一个应用程序。
只是想法的样本:
public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Use(async (context, next) =>
{
if (context.Request.Path.Value.Contains("/api"))
{
string BaseUrl = (env.IsDevelopment() ? "172.0.0.1:5001" : "192.168.0.10:5001") + context.Request.Path.Value + (context.Request.QueryString.HasValue ? context.Request.QueryString.Value : "");
var handler = new HttpClientHandler
{
//handle Ssl
ClientCertificateOptions = ClientCertificateOption.Manual,
SslProtocols = SslProtocols.Tls12,
UseDefaultCredentials = true,
ServerCertificateCustomValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true),
};
//handle compression
if (handler.SupportsAutomaticDecompression)
{
handler.AutomaticDecompression = DecompressionMethods.GZip |
DecompressionMethods.Deflate;
}
//handle Ssl
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Clear();
//handle Authorization Header
if (context.Request.Headers.ContainsKey("Authorization"))
{
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", context.Request.Headers["Authorization"].ToString());
}
// forward User-Agent
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", context.Request.Headers["User-Agent"].ToString());
HttpResponseMessage response = null;
if (context.Request.Method == "GET")
{
response = await client.GetAsync(BaseUrl);
}
if (context.Request.Method == "POST")
{
try
{
string documentContents;
using (Stream receiveStream = context.Request.Body)
{
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
documentContents = readStream.ReadToEnd();
}
}
// HttpContent content = new StringContent(documentContents, Encoding.UTF8, "application/json");
response = await client.PostAsync(BaseUrl, content);
}
catch (Exception e)
{
await context.Response.WriteAsync(e.Message, Encoding.UTF8);
}
}
if (response != null)
{
foreach (var header in response.Content.Headers)
{
StringValues stringValues = StringValues.Empty;
foreach (var values in header.Value)
{
StringValues.Concat(stringValues, values);
}
context.Response.Headers.Add(header.Key, stringValues);
}
context.Response.StatusCode = (int)response.StatusCode;
var body = await response.Content.ReadAsStringAsync();
await context.Response.WriteAsync(body, Encoding.UTF8);
}
}
else
{
await next.Invoke();
}
});
}
}
我认为你必须根据到不同目的地的路径分开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.