Skip to main content

How to upload files by using ASP.NET Core

Many web applications require users to upload files. Whether it’s a photo, document or any other type of file, your application should be able to read it from the client computer and store it in the server.

I have recently worked on a case where the developer were trying to use the functions below to get the full path of the file:

string filePath = Path.Combine(Request.Form["file"].ToString());
string filePath = System.IO.Path.GetFullPath(Request.Form["file"].ToString());

These functions won’t return the full path if “Include local directory path when uploading files to a server” setting is disabled in Internet Explorer.

Read More

How to encrypt and decrypt connection strings

Web applications use connection strings to find out what database to interact with which credentials and other configuration. For example, you can instruct your web application to use X database at the server with 192.168.1.100 IP address by using Z username and Y password.

For most applications, the connection strings are stored in web.config files. It means your IP address, database name, username, and password are stored as a clear text in a file. It may not be a problem for your Development server but it is definitely a security concern for your Production servers. This is where we need to encrypt these connection strings.

Read More

SocketException: No such host is known

Visual Studio provides all-in-one experience for software developers. From coding to testing, it makes IT professional’s lives easier. Error messages like “Socket Exception: No such host is known” refers to certain issues in the code or configuration of the application. In this post, I will explain the root cause and solution for this error message.

This error message may appear in may forms. Here is one of them:

System.Net.Sockets.SocketException: No such host is known

Error "No such host is known"

In some cases, you may see the exception code 0x80004005 along with the error message above. In addition, if you are receiving ThreadAbortException, check this post out.

The root cause of the “No such host is known” error

In the scenario I tested and in the online posts I reviewed, the root cause was the same: DNS name resolution failure. Basically, your application tries to call an API URL or to connect to a service but it is not able to find the hostname or your server is not able to do DNS lookup for that hostname.

Since it is an issue with DNS name resolution, nothing you do with Visual Studio, IIS, TFS or Windows will help fixing this issue. However, a simple code update may do the magic!

Similar issues on external sites: CodeProject, GitHub, Microsoft Support.

Another Visual Studio error: Remote Debugging Monitor (MSVSMON.EXE)

Solution for “No such host is known” error

The solution is fairly simple. Make sure your server is able to access to the DNS server and your DNS server is able to lookup the hostname in your code. Follow the steps below to troubleshoot and solve this issue.

  • In Command Prompt, run ipconfig /all. This will list the IP addresses of the DNS server you use. Try to ping them. Additionally, check with a colleague to see he or she has the same configuration
  • Run nslookup. Enter the hostname in your code. Make sure the DNS server is able to lookup this hostname.
  • Make sure there is no typo or white spaces in the hostname. If you are getting this input from a JSON call or configuration file, it is possible that the source string and the variable you have are different.

If this is an ASP.NET WebForms application, you may also come across Validation of viewstate MAC failed error. Follow the link for the specific solution for it.