I have multiple if statements here which would check if a request path starts with a given string and return true if the condition is met
if (context.Request.Path.StartsWithSegments("/abc/def", StringComparison.OrdinalIgnoreCase))
{ return true; }
if (context.Request.Path.StartsWithSegments("/pqr/xyz", StringComparison.OrdinalIgnoreCase))
{ return true; }
and so on with few more if statements….
How do I optimize this and create a list of such values ("/abc/def") where it would just iterate through the list and return true if the condition is met?
2
Answers
You can use
.Any()
with the list of expected path,You can Use
Array.Exists()
method for checking path isexpectedPath
array.