Pirate
Pirate

Reputation: 1185

.NET filename with # error - 404 - File or directory not found

I am working on .NET Core 2.2 application (will be upgraded soon). There is a functionality to upload files on the server which can be accessed by other users via a link. Everything works pretty fine. There are checks to prevent files with certain characters including #. The only issue I am having is, client is insisting to allow # in filename. There are no issues when uploading such files, but it doesn't load via link. I get Status Code: 404; Not Found error. This was the issue in legacy site (ASP.NET WebForms) as well where it was showing 404 - File or directory not found.. The URL I get looks like this: /_ClientData/NTTF/Announcements/61/Docs/invalid%20#%20test.pdf

As a last option, I can allow this files and replace # with something else on server, but I am wondering if there is any way to make this work without manipulating filename.

Upvotes: 0

Views: 1169

Answers (1)

paulsm4
paulsm4

Reputation: 121881

You probably have an Razor page (or equivalent) that dynamically generates the "clickable link".

SUGGESTION: Use HttpUtility.UrlEncode() to explicitly generate the link, as you serve the page.

It should generate something like this:

  • File Name: invalid # test.pdf
  • HttpUtility.UrlEncode: invalid%20%23%20test.pdf

Upvotes: 1

Related Questions