.net - Validate Uri using a ConfigurationValidator or other validator -


in custom configuration section of config file want have properties or elements define new scheme, host , port service endpoint, not define path. these should allowed https://newhost/ or http://newhost:800, not newhost:800, http://newhost/path/to/service.

what best option implement?

it feels there should overload of uri.parse, uriparser make easy. there urivalidator have missed? or regex going best option (so can disallow path)?

note isn't specific configurationvalidator general validator can reused useful.

there getleftpart method of uri class here.

getleftpart method

the getleftpart method takes enumeration uripartial system enumeration, 1 of (uripartial.authority) return:

the scheme , authority segments of uri.

this removes extraneous path information may in original string, , return zero-length string if uri supplied not contain valid scheme (i.e. http or https etc. parts of uri) and/or authority (i.e. in example, newhost part of uri).

from here, should able compare return value of call getlleftpart original uri string, , if they're different, uri "invalid". if they're same, uri "valid" (for purposes).

here's simple example class perform "validation" returning either true or false uri (both c# , vb.net versions):

c#

public static class urivalidator {     public static bool isurivalid(string uri)     {         try         {             string original_uri = uri;             if (uri.substring(uri.length - 1, 1) == "/")             {                 original_uri = uri.substring(0, uri.length - 1);             }             uri myuri = new uri(original_uri);             string new_uri = myuri.getleftpart(uripartial.authority);             if (original_uri.tolower() == new_uri.tolower())             {                 return true;             }             else             {                 return false;             }         }         catch         {             return false;         }                 } } 

vb.net

public class urivalidator     public shared function isurivalid(byval uri string) boolean         try             dim original_uri = uri             if uri.substring(uri.length - 1, 1) = "/"                 original_uri = uri.substring(0, uri.length - 1)             end if             dim myuri uri = new uri(original_uri)             dim new_uri string = myuri.getleftpart(uripartial.authority)             if original_uri.tolower = new_uri.tolower                 return true             else                 return false             end if         catch ex exception             return false         end try     end function end class 

i ran simple test using class:

console.writeline("https://newhost/" + "  " + urivalidator.isurivalid("https://newhost/")); console.writeline("http://newhost:800" + "  " + urivalidator.isurivalid("http://newhost:800")); console.writeline("newhost:800" + "  " + urivalidator.isurivalid("newhost:800")); console.writeline("newhost:" + "  " + urivalidator.isurivalid("newhost:")); console.writeline("qwerty:newhost" + "  " + urivalidator.isurivalid("qwerty:newhost")); console.writeline("qwerty://newhost" + "  " + urivalidator.isurivalid("qwerty://newhost")); console.writeline("qwerty://newhost:800" + "  " + urivalidator.isurivalid("qwerty://newhost:800")); console.writeline("http://newhost/path/to/service" + "  " + urivalidator.isurivalid("http://newhost/path/to/service")); 

and gave following output:

https://newhost/ true
http://newhost:800 true
newhost:800 false
newhost: false
qwerty:newhost false
qwerty://newhost true
qwerty://newhost:800 true
http://newhost/path/to/service false

which seems you're after!

note uri's qwerty://newhost still validate true qwerty could valid protocol registered on system. if wanted allow http and/or https, should trivial add in.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -