Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Language specific UseRewriter i startup .net core 7

Language specific UseRewriter i startup .net core 7

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdotnetcomxml
4 Posts 3 Posters 24 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tablet5
    wrote on last edited by
    #1

    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();

    Richard DeemingR K 3 Replies Last reply
    0
    • T Tablet5

      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();

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • T Tablet5

        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();

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        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 the UseRewriter 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
        

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        • T Tablet5

          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();

          K Offline
          K Offline
          Kin Bueno 2023
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups