Redirects in Program.cs vs. Web.config

Ohai :waving_hand:

I was running into some problems doing redirects/rewrites and became curious… so here’s hoping someone can “explain it to me like I’m five” :grin:

In Program.cs I have these lines:

var rewriteOptions = new RewriteOptions().AddRedirectToWwwPermanent();
app.UseRewriter(rewriteOptions);
app.UseHttpsRedirection();

By looking just a bit around, I can see that the RewriteOptions also have ways to add the HTTPS redirect – so why does this use a different method (on app directly)?

I’m used to controlling all of this in Web.config myself, which (IIRC) happens before Umbraco even sees the request (right?) So what’s the recommended approach here?

Thanks,
/Chriztian

Can still do it in web.config and IIS will pick that up as normal prior to the asp.net process..

rewriteoptions will do this in the asp.net process, so webhost agnostic… say on linux, or locally in kestrel for testing

MS recommends using the highest upstream handler for obv reasons.. if IIS deals with it before aspnet process then you save compute assets.. though might be moot depending on the rules you actually need…

When to use URL rewriting middleware

Use URL Rewriting Middleware when the following approaches aren’t satisfactory:

There are other options too IRules (more complex than a regexp with conditions) middleware intercept of the request etc…

you can also have your same we.config rules in an xml file and have the aspnet processs translate them into its rewriter too…

Also note that if you’re not hosting on IIS (and use Kestrel instead) then web.config redirects don’t work.