Link to Google search throws bad request (400) in IE - c#

I'm developing a C# MVC web-application where I'd like to directly link to a google search with a specific string. The final string is formatted in the following way: http://www.google.com/search?q=partialstring1+partialstring2+partialstring3
The html code looks as follows:
<p><br><a target="_blank" href=http://www.google.com/search?q=' + FormattedString + '> Google </a> ' + </p>
This html is again part of a final string which is should be called in an InfoBubble.
It works on any other browser (Firefox, Chrome, Opera ...) but it results in Error 400 when opening the link in Internet Explorer (11 and below).
It says Your client has issued a malformed or illegal request.
It works when I look up the specific string directly in Google and the resulting url looks exactly the same as the one I'd like to open from my application.
The formatted string gets rid of all umlauts (ä, ü, ö) and special characters (%&/§$) and works fine in Firefox when I copy and paste it there.
Is there some special IE-formatting for Google searches? I really don't know how to fix this issue, so any help would be appreciated.

I don't have any issue with loading the link in IE 11 at least. I have it setup like this with Razor formatting. Are you using something different for the views?
#{
var FormattedString = "partialstring1+partialstring2+partialstring3";
}
<p><br><a target="_blank" href="http://www.google.com/search?q=#FormattedString"> Google </a></p>
Which gives me a functional link in any browser.

Related

.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.
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

ASP.Net Core Returning 404 when image url spaces encoded as +

I have URLs stored in a database where the spaces are encoded as +.
When the browser requests these urls the web server returns a 404 response.
These URLs are all for static images stored on the web server in the wwwroot folder.
If I manually change the + for %20 then the image is returned correctly.
Is this a deliberate change in ASP.Net Core or is this a bug?
If it's deliberate, then it's going to be very painful for me going through the database and re-encoding all the URLs, many of which are embedded in HTML snippets (I know storing HTML in the DB or having spaces in image files aren't a good idea but it was done long before I joined the company and that's the state we're already in).
I'm using ASP.Net 2.1, running on .Net Framework.
It's running through IIS Express at the moment (during development) but will be deployed with full IIS.
I have seen this other question but it's specifically to do with API calls and the answer doesn't seem to be applicable to my question as there are no routes to change as I'm requesting static image files.
Edit: Extra detail
The html is output using #Html.Raw(html)
The resulting html output to the browser is of the form
<img src="/BorderThemes/grey+4px+rounded+corners_TL.png" />
The Html was generated on the server and then stored in the DB so we can be confident it's safe to output to the browser and, no, I have no idea why anyone would do that rather than building the HTML when it's needed but it was before my time and it's the situation I'm already in.
Update:
I've looked deeper into this and if I enter http://localhost:8000/BorderThemes/grey+4px+rounded+corners_TL.png into my web browser I get a page from IIS saying Http Error 404.11 saying that my URL is double encoded and linking to here for more information. This does include instructions on how to allow double-encoding but with warnings that it can have security consequences.
If I enter the url http://localhost:8000/BorderThemes/grey%204px%20rounded%20corners_TL.png I get an image back.
I was having issues with paths / html stored in the DB but after experimenting, it appears that System.Net.WebUtility.UrlEncode encodes spaces as +. For example WebUtility.UrlEncode("foo bar.png") returns foo+bar.png, which is rejected as double-encoded by IIS.
Am I missing something or is Microsoft's function for encoding URLs encoding the URLs in a way that Microsoft's web server rejects?
If you want %20 instead of + tryusing EscapeDataString to encode URI :
Uri.EscapeDataString(someString);
Refer https://stackoverflow.com/a/50682381/704008
But you have already generated url & can'e do anything now so try using HtmlDecode like
System.Net.WebUtility.HtmlDecode.HtmlDecode(html);
I am not sure it best to use with Raw or some method exists like decode in #Html but try using :
#Html.Raw(System.Net.WebUtility.HtmlDecode(html))
Refer:
https://learn.microsoft.com/en-us/dotnet/api/system.net.webutility?view=netstandard-2.0
https://learn.microsoft.com/en-us/dotnet/api/system.net.webutility.urldecode?view=netstandard-2.0#System_Net_WebUtility_UrlDecode_System_String_

Removing Localhost url in asp mvc

Just having a problem trying to properly display an image from an external site but mvc constraints links and automatically adds the localhost: url at the start of everything even with custom routing this cannot be avoided
eg I require: www.google.com/finance/chart?q=NYSE:V&tlf=12
but i am getting: http://localhost:3022/www.google.com/finance/chart?q=NYSE:V&tlf=12
any help would be much appreciated
Your problem is not MVC; it is the formation of your <a> tags. You are doing it like this:
blah...
You should be doing it like this:
blah...
Without including the protocol at the beginning, the browser assumes your link is relative to the current site. It has nothing whatsoever to do with MVC.
If you require a link on a separate domain, you need to add http://
So :
http://www.google.com/finance/chart?q=NYSE:V&tlf=12
Should work!
Why? Without the http, the link is considered relative and the browser uses the relative domain -> localhost!

HTTP Error 405 when Browsing to XML file

I have my app that is creating an XML File with XMLTextWriter class in ASP .Net C#. I create a nicely formed xml file, then spit out a link for the user. When I click on the link, I recieve an error saying that Page cannot be displayed:
"HTTP Error 405 - The HTTP verb used to access this page is not allowed."
However, at this point I place my cursor in the adress bar of the browser, and hit enter and Viola!, my nice xml file is displayed.
Why does it take an enter, in order to make my browser behave.
What can I do to to correct this so that when we click on the link the xml file displays without needing the extra enter in the browser address bar?
I went into my web server already and added the XML mime type to the registered mim types at the web server level.
I am wondering what could be the problem?
From the error message it seems there is something wrong with the link you are using.
View the source from your browser and verify that the tag is pointing to the correct location. Also, make sure it is only pointing to the one document and not doing anything else like calling javascript functions or posting back.

c# get complete URL with "#" [duplicate]

This question already has answers here:
Retrieving anchor link in URL for ASP.NET
(3 answers)
Closed 9 years ago.
I have run into a trivial(?) problem when trying to get the whole URL of a c# page.
The url contains the "#"-link ref char. And i would like that to when I grab the URL
Eg. http://localhost/site/page.aspx?var=1&var=2#link
I have tried Request.URL, Request.Querystring etc, it only returns up to the "#"-char.
Is there any way to grab even the last part?
Thanks in advance
That is not possible using server code only. The part after the # is not sent in the request at all, it never leaves the browser.
If you want the part after the # you have to copy it using Javascript before the request is sent to the server, and put the value in the querystring.
Your problem is that # specified an anchor in the page, so the browser sees:
http://localhost/site/page.aspx?var=1&var=2
And then looks in the page for
<a name="link">anchor</a>
As this is client side you need to escape the # from the URL - you can't get it on the server because the browser's already stripped it off.
Are you sure that the stuff after the # isn't sent to the server. I'm pretty sure i made a test with an ajax-app some years ago where the url could be copied and sent to people without javascript by only modifiying the stuff after the # in the url when browsing around with javascript enabled.
That was in PHP and the browser was probably IE6.

Categories