Im part of a team creating a server project in ASP.NET that provides a series of services to logged in members. We separated the server into components such as for authentication. Example of components are seen in the table below. This way its helps manage the project in terms of testing and other benefits.
+-----------------+-------------------+
| 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 |
+-----------------+-------------------+
The issue we are facing is that we want to to use only one url in the browser just like in the table below. We have already matched subdirectories with their respective component . For example, /login
should redirect to Authentication
component. If a user has navigated to the search page and entered a search term, it should redirect from www.mysite.com/search?value=test
to 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?
you can use a middleware for handle your request:
this middleware get request and send it to other application and get that response and forward to first one.
just sample for idea:
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();
}
});
}
}
I think you must separate base on path to separate destination.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.