昨天刚解决 asp.net core 中使用 AddIISUrlRewrite 时遇到 null 引用问题,今天又遇 null 引用异常,这次发生在 IISUrlRewrite.ConditionEvaluator.Evaluate 方法中
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite.ConditionEvaluator.Evaluate(ConditionCollection conditions, RewriteContext context, BackReferenceCollection backReferences)
请问如何解决?
在 Microsoft.AspNetCore.Rewrite 的实现源码 UrlRewriteFileParser.cs 中将
builder.ConfigureConditionBehavior(grouping, trackAllCaptures);
foreach (var cond in conditions.Elements(RewriteTags.Add))
foreach (var cond in adds)
{
ParseCondition(cond, builder, patternSyntax);
}
改为下面的代码可以解决这个问题
var adds = conditions.Elements(RewriteTags.Add);
if (adds == null || adds.Count() == 0)
{
return;
}
builder.ConfigureConditionBehavior(grouping, trackAllCaptures);
foreach (var cond in adds)
{
ParseCondition(cond, builder, patternSyntax);
}