Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How Do I Port My IIS Rewrite Rules to ASP.Net Core?

$
0
0

I am trying to port rewrite rules that worked just on a web forms site to my ASP.Net Core site. I was hoping I could do this easily by using something like this in my Startup.cs file:

using (StreamReader iisUrlRewriteStreamReader =
                File.OpenText("IISUrlRewrite.xml"))
            {
                var options = new RewriteOptions()
                    .AddIISUrlRewrite(iisUrlRewriteStreamReader);
                app.UseRewriter(options);
            }

And then use the same rules by adding the following to the IISUrlRewrite.xml file:

<?xml version="1.0" encoding="utf-8" ?><rewrite><rules><rule name="Force non-WWW" enabled="true" stopProcessing="true"><match url="(.*)" /><conditions logicalGrouping="MatchAny"><add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" /></conditions><action type="Redirect" url="https://{C:2}/{R:1}" appendQueryString="true" /></rule><rule name="Rewrite subdomains"><match url=".*" /><conditions><add input="{HTTP_HOST}" pattern="^(^$|[^\-]*)(^$|[\-]?)([^\-]\w)\.postalmostanything\.com$" /><add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /></conditions><action type="Rewrite" url="{ToLower:/{C:3}{C:2}{C:1}{PATH_INFO}}" appendQueryString="true" /></rule></rules></rewrite>

Unfortunately the application wont run and the log files says:

crit: Microsoft.AspNetCore.Hosting.Diagnostics[6]
Application startup exception
System.FormatException: Unrecognized parameter type: 'PATH_INFO', terminated at string index: '35'
at Microsoft.AspNetCore.Rewrite.IISUrlRewrite.ServerVariables.FindServerVariable(String serverVariable, ParserContext context, UriMatchPart uriMatchPart, Boolean alwaysUseManagedServerVariables)
at Microsoft.AspNetCore.Rewrite.IISUrlRewrite.InputParser.ParseParameter(ParserContext context, IList`1 results, UriMatchPart uriMatchPart)
at Microsoft.AspNetCore.Rewrite.IISUrlRewrite.InputParser.ParseString(ParserContext context, UriMatchPart uriMatchPart)
at Microsoft.AspNetCore.Rewrite.IISUrlRewrite.InputParser.ParseParameter(ParserContext context, IList`1 results, UriMatchPart uriMatchPart)
at Microsoft.AspNetCore.Rewrite.IISUrlRewrite.InputParser.ParseString(ParserContext context, UriMatchPart uriMatchPart)
at Microsoft.AspNetCore.Rewrite.IISUrlRewrite.InputParser.ParseInputString(String testString, UriMatchPart uriMatchPart)
at Microsoft.AspNetCore.Rewrite.IISUrlRewrite.UrlRewriteFileParser.ParseUrlAction(XElement urlAction, UrlRewriteRuleBuilder builder, Boolean stopProcessing)
at Microsoft.AspNetCore.Rewrite.IISUrlRewrite.UrlRewriteFileParser.ParseRuleAttributes(XElement rule, UrlRewriteRuleBuilder builder)
at Microsoft.AspNetCore.Rewrite.IISUrlRewrite.UrlRewriteFileParser.ParseRules(XElement rules, IList`1 result, Boolean global)
at Microsoft.AspNetCore.Rewrite.IISUrlRewrite.UrlRewriteFileParser.Parse(TextReader reader, Boolean alwaysUseManagedServerVariables)
at Microsoft.AspNetCore.Rewrite.IISUrlRewriteOptionsExtensions.AddIISUrlRewrite(RewriteOptions options, TextReader reader, Boolean alwaysUseManagedServerVariables)
at PostAlmostAnything.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in C:\Users\sulli\source\repos\PostAlmostAnything\PostAlmostAnything\Startup.cs:line 80
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.<UseStartup>b__2(IApplicationBuilder app)
at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\me\Documents\postalmostanything
info: Microsoft.Hosting.Lifetime[0]
Application is shutting down...

Is there some way to add support for PATH_INFO? Do I need to skip the using the rewriting middleware in the application entirely and just add those rewrite rules via IIS after publishing the application? 

It seems like the redirect to non www works ok by itself but the rewrite rules for the subdomains don't work. I tried modifying it not to include PATH_INFO but that just loads the home page even thought I have routing enabled to handle passing parameters via URLs like the ones that the subdomains should be rewritten to.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>