Home
%3CLINGO-SUB%20id%3D%22lingo-sub-984827%22%20slang%3D%22en-US%22%3ESystem.UriFormatException%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-984827%22%20slang%3D%22en-US%22%3E%3CP%3ESystem.UriFormatException%26nbsp%3Bwith%26nbsp%3B%3CSTRONG%3EInvalid%20URI%3A%20The%20format%20of%20the%20URI%20could%20not%20be%20determined%3C%2FSTRONG%3E%20message%20is%20one%20of%20the%20errors%20that%20is%20almost%20always%20related%20to%20code.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20the%20stack%20trace%20and%20description%20from%20a%20DebugDiag%20dump%3A%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CBR%20%2F%3ESystem.UriFormatException%3CBR%20%2F%3EInvalid%20URI%3A%20The%20format%20of%20the%20URI%20could%20not%20be%20determined.%3CBR%20%2F%3E%3CEM%3Eat%20System.Uri.CreateThis(String%20uri%2C%20Boolean%20dontEscape%2C%20UriKind%20uriKind)%3C%2FEM%3E%3CEM%3E%3CBR%20%2F%3Eat%20Varonis.EventHandlerAuditor.WebApplicationConfiguration.IsWebApplicationMatches(String%20webApplicationUrl)%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F154962iBF732871467C0B52%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_0.png%22%20title%3D%22clipboard_image_0.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1655986791%22%20id%3D%22toc-hId-1656009856%22%3ERoot%20cause%3C%2FH3%3E%0A%3CP%3EIf%20the%20input%20value%20is%20not%20what%20the%20application%20expected%2C%20this%20exception%20is%20generated.%20In%20other%20words%2C%20the%20application%20was%20expecting%20a%20valid%20URI%20but%20the%20value%20it%20received%20wasn%E2%80%99t%20in%20URI%20format.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20my%20case%2C%20the%20value%20was%20%E2%80%9C%3CSTRONG%3ESERVER01%3C%2FSTRONG%3E%E2%80%9C%20which%20is%20not%20a%20valid%20URI%20so%20that%20the%20application%20threw%20an%20error%20at%20the%20last%20line%20below.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Epublic%20static%20bool%20IsWebApplicationMatches(string%20webApplicationUrl)%0A%7B%0A%20%20if%20(string.IsNullOrEmpty(webApplicationUrl))%0A%20%20%20%20return%20true%3B%0A%0A%20%20Uri%20uri%20%3D%20new%20Uri(webApplicationUrl)%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--896170170%22%20id%3D%22toc-hId--896147105%22%3ESolution%3C%2FH3%3E%0A%3CP%3EThis%20issue%20may%20occur%20because%20of%20user%20behavior%20(the%20value%20submitted%20into%20the%20function).%20No%20matter%20what%20the%20root%20cause%20is%2C%20the%20application%20should%20catch%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.uriformatexception%3Fview%3Dnetframework-4.8%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ethe%20exception%3C%2FA%3E.%20Implement%26nbsp%3B%3CSTRONG%3Etry-catch%3C%2FSTRONG%3E%26nbsp%3Bblock%20in%20that%20function%20to%20handle%20this%20exception.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt%20also%20helps%20finding%20the%20assembly%20that%20is%20throwing%20the%20error%20(look%20at%20stack%20trace%20for%20this%20information).%20If%20it%E2%80%99s%20a%20third-party%20component%2C%20you%20will%20need%20to%20contact%20them.%20They%20might%20already%20have%20a%20fix%20or%20updated%20assembly.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20also%20try%20to%20figure%20out%20how%20that%20value%20is%20passed%20to%20that%20function.%20Review%20the%20inputs%20and%20use-cases.%20Ask%20your%20user%20about%20the%20steps%20to%20reproduce%20it.%20%26nbsp%3BIt%20could%20be%20because%20of%20a%20user%20who%20enters%20invalid%20information%20in%20a%20form.%20However%2C%20no%20matter%20what%20the%20input%20is%2C%20the%20application%20should%20catch%20the%20exception.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-984827%22%20slang%3D%22en-US%22%3E%3CP%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3ESystem.UriFormatException%20with%20Invalid%20URI%3A%20The%20format%20of%20the%20URI%20could%20not%20be%20determined%20message%20is%20one%20of%20the%20errors%20that%20is%20almost%20always%20related%20to%20code.%3CBR%20%2F%3E%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

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.