System.UriFormatException

This post has been republished via RSS; it originally appeared at: IIS Support Blog articles.

System.UriFormatException with Invalid URI: The format of the URI could not be determined message is one of the errors that is almost always related to code.

 

Here is the stack trace and description from a DebugDiag dump:


System.UriFormatException
Invalid URI: The format of the URI could not be determined.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at Varonis.EventHandlerAuditor.WebApplicationConfiguration.IsWebApplicationMatches(String webApplicationUrl)

clipboard_image_0.png

Root cause

If the input value is not what the application expected, this exception is generated. In other words, the application was expecting a valid URI but the value it received wasn’t in URI format.

 

In my case, the value was “SERVER01“ which is not a valid URI so that the application threw an error at the last line below.

 

public static bool IsWebApplicationMatches(string webApplicationUrl) { if (string.IsNullOrEmpty(webApplicationUrl)) return true; Uri uri = new Uri(webApplicationUrl); }

 

Solution

This issue may occur because of user behavior (the value submitted into the function). No matter what the root cause is, the application should catch the exception. Implement try-catch block in that function to handle this exception.

 

It also helps finding the assembly that is throwing the error (look at stack trace for this information). If it’s a third-party component, you will need to contact them. They might already have a fix or updated assembly.

 

You can also try to figure out how that value is passed to that function. Review the inputs and use-cases. Ask your user about the steps to reproduce it.  It could be because of a user who enters invalid information in a form. However, no matter what the input is, the application should catch the exception.

REMEMBER: these articles are REPUBLISHED. Your best bet to get a reply is to follow the link at the top of the post to the ORIGINAL post! BUT you're more than welcome to start discussions here:

This site uses Akismet to reduce spam. Learn how your comment data is processed.