Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 522329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:20:33+00:00 2026-05-13T08:20:33+00:00

HttpServerUtility contains a public function called UrlEncode. It is not a shared function. HttpServerUtility

  • 0

HttpServerUtility contains a public function called UrlEncode. It is not a shared function. HttpServerUtility does not have any public constructors.

Doing this fails:

Dim encodeMe As String = "a string to be encoded!"
HttpServerUtility.UrlEncode(encodeMe) 'Bombs out

This works, and is how Microsoft says to do it:

Dim instance As HttpServerUtility
Dim encodeMe As String = "a string to be encoded!"

instance.UrlEncode(encodeMe ) 'Works!

How did they accomplish this? You can’t instantiate an instance of it using a constructor, yet you can’t access UrlEncode by just referencing HttpServerUtility.UrlEncode.

EDIT: While I thoroughly enjoyed everyone getting into a big OO debate, I believe the problem is faulty MSDN documentation. The line “Dim instance As HttpServerUtility” should read “Dim instance As HttpServerUtility = Context.Server” The code which I included (which is from the MSDN documentation) does not actually work, and instead throws a null reference exception – just as you’d expect. Thank you, Jason!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:20:34+00:00Added an answer on May 13, 2026 at 8:20 am

    Are you sure this works?

    Dim instance As HttpServerUtility
    Dim encodeMe As String = "a string to be encoded!"
    instance.UrlEncode(encodeMe) 'Works!
    

    This will give you a NullReferenceException at runtime (and the compiler will give you a warning that instance is not being assigned to). Seriously, Microsoft didn’t do anything here. The above code is disastrously wrong and will die at runtime.

    And you can’t do this

    Dim encodeMe As String = "a string to be encoded!"
    HttpServerUtility.UrlEncode(encodeMe) 'Bombs out
    

    because UrlEncode is not defined as a Shared method in HttpServerUtility.

    You need a non-null instance of HttpServerUtility. The right way to use HttpServerUtility is like this:

    Dim instance As HttpServerUtility = HttpContext.Server
    Dim s As String = "Hello, World!"
    Dim result As String = instance.UrlEncode(s)
    

    Another option is to just use HttpUtility for which there is a Shared method HttpUtility.UrlEncode:

    Dim s As String = "Hello, World!"
    Dim result As String = HttpUtility.UrlEncode(s)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm recieving the following error message, A public action method 'RenderMenu' was not found
What's the difference between HttpServerUtility.UrlPathEncode and HttpServerUtility.UrlEncode ? And when should I choose one
I have read so many topics on this very subject by now, I can't
I imported system.web into my class lib project. Trying to find: system.web.httpserverutility.server.mappath But I
I'm trying to call the HttpServerUtuility.URLDecode function in C# using Visual Studio 2005, but
I have two files at the same location but the big one, when is
I want to write a unit test which tests the function of a class
I have a large .NET 2 web application that has been updated to target
How can I access the HttpServerUtility instance in ASP.NET MVC controller?
I would like to use Server.UrlEncode in .ashx . I try with the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.