0

I'm trying to filter out .asmx requests by using

string path = request.Url.AbsolutePath;
return Path.HasExtension(path) && Path.GetExtension(path).Equals(".asmx", StringComparison.InvariantCultureIgnoreCase);

However Path.GetExtension is always returning an empty string on my web service requests. The path for the web service request is along the lines of

https://www.mywebsite.co.uk/webservices/some-service.asmx/SomeMethod

To get around it I'm using

return path.IndexOf(".asmx", StringComparison.InvariantCultureIgnoreCase) >= 0; 

But wondered if there was a way of getting Path.GetExtension to work.

GMon
  • 638
  • 6
  • 14
  • 2
    The problem is that `AbsolutPath` still contains `"/SomeMethod"`. And that does not have an extension. You'd need to know which part of the URI is the file to use `GetExtension`. – René Vogt Aug 05 '16 at 10:27
  • Does Path.GetExtension just get the ending .xxx as the extension? Is there a difference between me finding a part of the URI to use GetExtension and just using indexOf(".asmx")? – GMon Aug 05 '16 at 10:46
  • 1
    If you have no good way of finding the relevant part of the uri and have no fear that ".asmx" appears somewhere else for other (false positive) reasons using `indexOf` seems ok. The [reference source](http://referencesource.microsoft.com/#mscorlib/system/io/path.cs,f424e433705aeb09) shows that `GetExtension` simply checks that it's a valid path and returns the trailing ".*" or an empty string if there is no "." (after the last separator like "/" or "\"). – René Vogt Aug 05 '16 at 10:54
  • I think indexOf should be fine then as I don't think it will appear anywhere else other than a web service call so I'll use that. Thanks! – GMon Aug 05 '16 at 10:58

1 Answers1

0

Use the code in this link to get the URI of the .asmx page.

Then get the file path of the page using that URI.

NOTE, when on .asmx pages, u have to use the HttpContext qualifier, since .asmx files don't inherit the Page class.

Community
  • 1
  • 1
Mr. Whales
  • 11
  • 4