Language specific UseRewriter i startup .net core 7
-
Im trying to trick my startup to add AddIISUrlRewrite by different domains, I not sure it's possible, any ideas? I'm Not really sucessfull. :laugh:
var options = new RewriteOptions();
bool blnsiteA = false;var tempoptions = new RewriteOptions();
tempoptions.Add(rewriteContext =>
{
var request = rewriteContext.HttpContext.Request;
if (request.Host.Host == "siteA.com")
{
blnsiteA = true;
}
});if (blnsiteA)
{
options.AddIISUrlRewrite(env.ContentRootFileProvider, "RewritesSiteA.xml");
}
else
{
options.AddIISUrlRewrite(env.ContentRootFileProvider, "RewritesSiteB.xml");
}options.AddRedirectToNonWww();
app.UseRewriter(options);
app.UseStaticFiles(); -
Im trying to trick my startup to add AddIISUrlRewrite by different domains, I not sure it's possible, any ideas? I'm Not really sucessfull. :laugh:
var options = new RewriteOptions();
bool blnsiteA = false;var tempoptions = new RewriteOptions();
tempoptions.Add(rewriteContext =>
{
var request = rewriteContext.HttpContext.Request;
if (request.Host.Host == "siteA.com")
{
blnsiteA = true;
}
});if (blnsiteA)
{
options.AddIISUrlRewrite(env.ContentRootFileProvider, "RewritesSiteA.xml");
}
else
{
options.AddIISUrlRewrite(env.ContentRootFileProvider, "RewritesSiteB.xml");
}options.AddRedirectToNonWww();
app.UseRewriter(options);
app.UseStaticFiles();That won't work; the startup code runs when the application first starts, whereas the redirection code executes for each request. Your code will only ever apply the redirections for site B. Instead, you'll need to add conditions to your rewrite rules base on the
{HTTP_HOST}
- for example:URL Rewrite Module Configuration Reference | Microsoft Learn[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Im trying to trick my startup to add AddIISUrlRewrite by different domains, I not sure it's possible, any ideas? I'm Not really sucessfull. :laugh:
var options = new RewriteOptions();
bool blnsiteA = false;var tempoptions = new RewriteOptions();
tempoptions.Add(rewriteContext =>
{
var request = rewriteContext.HttpContext.Request;
if (request.Host.Host == "siteA.com")
{
blnsiteA = true;
}
});if (blnsiteA)
{
options.AddIISUrlRewrite(env.ContentRootFileProvider, "RewritesSiteA.xml");
}
else
{
options.AddIISUrlRewrite(env.ContentRootFileProvider, "RewritesSiteB.xml");
}options.AddRedirectToNonWww();
app.UseRewriter(options);
app.UseStaticFiles();The other option would be to use your own middleware. You can see the source of the
RewriteMiddleware
class on GitHub: aspnetcore/RewriteMiddleware.cs at b1dcacabec1aeacef72c9aa2909f1cb49993fa73 · strykerin/aspnetcore · GitHub[^] And the source of theUseRewriter
method: aspnetcore/RewriteBuilderExtensions.cs at b1dcacabec1aeacef72c9aa2909f1cb49993fa73 · strykerin/aspnetcore · GitHub[^] It shouldn't be too hard to build your own - something like this (untested):public static class RewriteBuilderExtensions
{
public static IApplicationBuilder UseMultiRewriter(
this IApplicationBuilder app,
IReadOnlyList<(Func predicate, RewriteOptions options)> optionsMap)
{
ArgumentNullException.ThrowIfNull(app);
ArgumentNullException.ThrowIfNull(optionsMap);
return app.UseMiddleware(Options.Create(optionsMap));
}
}public static partial class MultiRewriteMiddlewareLoggingExtensions
{
[LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = "Request did not match any rewrite rule set. Current url is {CurrentUrl}")]
public static partial void MultiRewriteMiddlewareNoMatchingRules(this ILogger logger, string CurrentUrl);\[LoggerMessage(EventId = 1, Level = LogLevel.Debug, Message = "Request is continuing in applying rules. Current url is {CurrentUrl}")\] public static partial void MultiRewriteMiddlewareRequestContinueResults(this ILogger logger, string CurrentUrl); \[LoggerMessage(EventId = 2, Level = LogLevel.Debug, Message = "Request is done processing. Location header '{Location}' with status code '{StatusCode}'.")\] public static partial void MultiRewriteMiddlewareRequestResponseComplete(this ILogger logger, string Location, int StatusCode); \[LoggerMessage(Ev
-
Im trying to trick my startup to add AddIISUrlRewrite by different domains, I not sure it's possible, any ideas? I'm Not really sucessfull. :laugh:
var options = new RewriteOptions();
bool blnsiteA = false;var tempoptions = new RewriteOptions();
tempoptions.Add(rewriteContext =>
{
var request = rewriteContext.HttpContext.Request;
if (request.Host.Host == "siteA.com")
{
blnsiteA = true;
}
});if (blnsiteA)
{
options.AddIISUrlRewrite(env.ContentRootFileProvider, "RewritesSiteA.xml");
}
else
{
options.AddIISUrlRewrite(env.ContentRootFileProvider, "RewritesSiteB.xml");
}options.AddRedirectToNonWww();
app.UseRewriter(options);
app.UseStaticFiles();not that CSSI are used on but not all [CIPD] are used on concurrent as alternative to event driven programming as procedure, to SYSAD, not construe to sitel as :: STD Cryphos negative asp