ASP.NET MVC - Illegal characters in path - redirect from Application_Error - asp.net-mvc

In my application I generated few url which contained " (double quotes), unfortunately.
I corrected the url's but google bot already indexed wrong urls.
Now I get quite often following exception:
System.ArgumentException: Illegal characters in path.
The only one place I can catch the error is the Application_Error in the Global.asax - I clean up the url over there and try to redirect to correct url - unfortunately it is not working... The application is not redirecting and I don't know why... Any suggestions?

Related

Sitecore SXA error illegal characters in path

Any one have solution for this issue .
With Sitecore Solution without SXA Adding empty attribute to httpRunTime tag in web.config file >> requestPathInvalidCharacters="", can fix issue special characters in URL and redirect you to "not found page" insted of 400 error page with message "A potentially dangerous Request.Path value was detected from the client()"
URL like : http://abc/* or http://abc/& >> redirect to item not found page and I replaced it with custom error page . So, NO issue with sitecore site without SXA.
I tried same solution with [sitecore SXA] site but not effected, It go to another error page with message >> illegal characters in path, Also I applied this solution https://kb.sitecore.net/articles/059908, But nothing effect, I still have the same error!!
Any solution, I looking for same behavior in SXA site
I fixed it by using URl rewrite in IIS and created redirect rule to 404 page hosted in site core

asp.net mvc file name with & in url throws file not found exception

when i am trying to set a href of anchor tag with "&" in file name, and clicking on it, the server returns 404 not found exception. my url looks like below:
<a href="www.domain.com/a&b.txt"
although The file "a&b.txt" stored at the location, the 404 error occurs, it seems that the "&" in url understands the qurystring key.
Please help!!!
You will need to URL Encode the querystring parameter values using the HttpUtility.UrlEncode Method

MVC .NET Urls aren't routed using the RouteCollection

We're using MVC .NET and the RouteCollection class to route URLs in our web app. This functions normally until we pass a URL containing the the text "PRN" anywhere inside the URL. When this happens, the routing will not occur and a 400 Page Not Found error is returned to the client. It's like something is throwing the error before the routing collection is even consulted, because the route the URL should take is never touched (by that I mean the underlying code's break-point is never hit, though the exact same URL without the string "PRN" will hit the break-point).
So I thought it might be a page validation issue, that maybe Microsoft decided to throw exceptions when the URL contains the phrase "PRN" because it is like "print" or "porn" but if that were the case then we'd see the "A potentially dangerous Request.Form value was detected from the client" error, but we don't.
Researching this has been a hassle because Google thinks PRN should return results for "porn", which means 98% of my search results are invalid (and inappropriate). Using the "-porn" clause in Google drops your results down to about 10-30 hits, all useless.
Does anyone know why a URL containing the string "PRN" will not route properly? If you have any posts or threads to point me to, that would be awesome (again, Google has failed me).

Rails 404 handler for non-Rails URLs

I've inherited a site with hundreds of scattered HTML and non-framework PHP files, which I am porting to Ruby on Rails 3.0.
As functionality is added in the Rails app, the corresponding pages are deleted from the document root; but, because there are often links to these in Google or from external sites, simply returning a 404 is not acceptable.
A URL like '/contact.php' should redirect to '/app/contact/', for example.
For the first few cases of this, I created simple stub html files at the old locations, with Meta tags within to perform the redirect. This doesn't scale well, particularly once I start replacing product pages, of which there are thousands.
My preference is to delete the old pages, then have the 404 handler dispatch these to the new Rails app, which will examine the URL using regexes and database lookup to try to figure out what the replacement page is, then issue a 301 redirect to that new page.
In httpd.conf, I placed the directive:
ErrorDocument 404 /app/error/handle404
# /app/error is a rails url.
When I hit "http://localhost/does-not-exist", this causes my ErrorController to be invoked, as expected.
However, within the controller, I cannot find the original path ("/does-not-exist") anywhere in request, request.headers, or ENV - I've been calling likely methods like request.request_uri (which contains /app/error/handle404), and examining request.headers and ENV without finding the expected original path.
The Apache access_log shows only the request for /does-not-exist, indicating that it transparently invoked /app/error/handle404 (without doing a redirect or causing a second request to be made).
How can I get access to the original URL?
Edit: to clarify, here is the sequence of events:
User hits legacy path like http://mysite/foo.php, probably coming from some ancient link from a blog.
...but foo.php no longer exists!
this is a 404, thus Apache invokes ErrorDocument
directive is "ErrorDocument 404 /railsapp/error/handle404"
Rails routes this to ErrorController action "handle404" - this is working correctly
problem: in ErrorController, request.request.uri, request.headers do not provide any clue as to which URL the user was actually trying to get to, like "/foo.php"; I need to know the original URL to serve up an appropriate replacement page.
As I couldn't find the original, non-rewritten URL in the Rails request, I ended up doing it in PHP - plain, old-fashioned, non-framework PHP with explicit mysqli_*() calls.
The PHP error handler receives the necessary information in the $_SERVER hash; $_SERVER['REQUEST_URI'] contains the original URI that I needed.
I look this up in a database, and if I find a corresponding entry, issue a 301 redirect to the new location; if there's no entry, I simply display a 404 page to the user.
Simplified (PHP):
$url = $_SERVER['REQUEST_URI'];
$redir = lookupRedirect($url); # database stuff here
if (! $redir) {
include ('404.phtml');
} else {
header("Status: 301");
header("Location: " . $redir['new_url']);
}
It's an ugly kluge, but I just couldn't find a way to make the Rails app aware of the error URL.

ASP.NET MVC - Search Criteria as Route Parameter in stead of querystring - Illegal Characters

I asked a previous question on how to optimize my search for SEO and users without JavaScript, and I figured out my answer by using RedirectToAction
However, in doing so, I've found a new issue that I need to resolve.
If I submit a search
"the quick brown fox jumped over the lazy dogs"
(trying to mimic Google with the "quotes" for complete phrases)
The application blows up on me (YSOD)
Illegal characters in path.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
The url looks like this (Firefox)
http://localhost/search/"the quick brown fox jumped over the lazy dogs"
I tried using Url.Encode but that didn't work either... the url looks like
http://localhost/search/%2522the%2bquick%2bbrown%2bfox%2bjumped%2bover%2bthe%2blazy%2bdogs%2522
and the error says
Server Error in '/' Application.
HTTP Error 400 - Bad Request.
It must be something very obvious that I'm missing.
Exception Details: System.ArgumentException: Illegal characters in path.
Looking at the form in your previous question I would have expected searchTerm to be a query string parameter and not part of the route.
http://localhost/search?searchTerms=..
If you want search to be part the routes, you will have to setup the routing correctly to.
If you provide more of the stack trace or your routing configuration you may get a better answer. Hope this helps anyway.

Resources