views:

2729

answers:

10

I'm trying to create something similar to the diggbar : http://digg.com/http://cnn.com

I'm using Visual Studio 2010 and Asp Development server.

However, I can't get the ASP dev server to handle the request because it contains "http:" in the path. I've tried to create an HTTPModule to rewrite the URL in the BeginRequest , but the event handler doesn't get called when the url is http://localhost:5957/http://yahoo.com. The event handler does get called if the url is http://localhost:5957/http/yahoo.com

To summarize

Any ideas?

+1  A: 

You need to use HttpUtility.UrlEncode on your string before redirecting to it.

Dave Markle
The issue is that the user is entering the string into the browser address bar. So the user may enter http://digg.com/http://cnn.com for example. There is no opportunity to encode the url in that scenario
mat3
http://digg.com/http://cnn.com
mat3
A: 

I'm not sure what web server digg is using but I'm betting this simply isn't possible with IIS or the built in web server for VS. Most likely it's a web server that can be modified to allow all sorts of wacky URLs.

Spencer Ruport
Yeah, it does seem like the request is getting thrown out at the web server level. Seems like a strange limitation though. Any MSFT IIS/ ASP.Net folks who can shed some light on if this limitation is present in the code base?
mat3
Spencer, Thanks for fixing the urls. There's one left right at the top: http://digg.com/cttp://cnn.com. Much appreciated.
mat3
Done...............
Spencer Ruport
A: 

This isn't IIS failing, its the ASP Development server. Try deploying to IIS and see if it works.

ck
I second this notion - the ASP.NET Development server dosent really compare to IIS, its drastically cut down.
pzycoman
I have the same problem with IIS 7, so I don't think it's just a dev server issue.
Ryan Riley
A: 

I'm pretty sure you could do it with IIS rewrite. ASP.NET Development Server can't do rewrite as far as I know but you could try doing it by using either IIS7 rewriter or if you have an earlier version, by the Ionics ISAPI Rewrite Filter.

DrJokepu
+4  A: 

From Stefan on the ASP.Net team: http://forums.asp.net/p/1431148/3221542.aspx

In current versions of ASP.NET Urls containing characters like the colon character will be rejected as a potential security threat. The historical reason for this is that the underlying NTFS file system supports alternate resource streams that can be accessed with names like "yourfile.txt:hiddendata.txt". Blocking the colon character from Urls prevents poorly written applications from accidentally working with alternate resource streams.

There is also a limitation in current versions of ASP.NET that incoming Urls need to map to the NTFS file system for purposes of determining managed configuration data.

In ASP.NET 4 these limitations can be optionally removed. However these changes are in the Beta 2 version of ASP.NET 4 - they are not in Beta 1. We tried out the Url listed earlier in this forum post and confirmed that with our internal builds of ASP.NET 4 you can use that style of Url and process it without any 400 error.

-Stefan

mat3
+2  A: 

I've had exactly this problem in creating a URL shortener for ASP.net. For the life of me, I could not get the colon encoded in such a way that ASP would accept it, using either HttpUtility.UrlEncode or javascript's escape() on the client-side.

My solution what to do Base64 encoding. Turns the whole thing into non-controversial alpha-numerics. Then you decode on the server. I used these functions.

Also, I created an HttpModule to be triggered by a leading hyphen so I know to handle what follows as a URL to be decoded. Ping me if you want further details.

Matt Sherman
Base64 encoding/decoding is a clever approach, but it sure does lead to some unsightly URLs.
Scott Mitchell
+5  A: 

From Stefan on the ASP.Net team

I snipped the following from a forthcoming What's New document covering Beta 2:

ASP.NET 4 introduces new options for expanding the range of allowable application Urls. The simplest and most useful change is that ASP.NET gives developers the option to allow longer Urls. Previous versions constrained Url path lengths to 260 characters (the NTFS file path limit). In ASP.NET 4 developers have the option of increasing (or decreasing if they choose) this limit as appropriate for their applications using two new httpRuntime configuration attributes:

Modify the value for "maxRequestPathLength" to allow longer or shorter Url paths (the portion of the Url sans protocol, server and query-string). Modify the value for "maxQueryStringLength" to allow longer or shorter query strings.ASP.NET 4 also enables developers to adjust the set of characters used by ASP.NET's Url character checks. When ASP.NET finds an invalid character in the path portion of a Url it rejects the request with an HTTP 400 error. In previous versions the Url character checks were limited to a fixed set of characters. In ASP.NET 4 developers can customize the set of character checks using another new httpRuntime configuration attribute:

By default the "requestPathInvalidChars" attribute contains seven characters considered invalid (the less than and greater than signs as well as the ampersand are encoded since configuration is an Xml file). Developers can then expand or reduce the set of invalid characters as appropriate for their application's needs. Note that ASP.NET 4 will still reject any Url paths that contain characters in the ASCII character range of 0x00-0x1F since those are considered invalid Url characters (RFC 2396 considers these characters invalid, and on Windows Servers running IIS6 or higher the http.sys protocol device driver automatically rejects Urls with these characters as well).

  • Stefan
mat3
A: 

The KB article 826437 ( http://support.microsoft.com/kb/826437 ) contains an answer to your question

  1. Ensure Microsoft .NET 1.1 SP1 is installed on the machine
  2. In the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET create a 32bit DWORD value of VerificationCompatibility = 1
  3. Restart IIS.

It worked for me (IIS 6, ASP.NET 4), probably it will work for you, too

wizzard0
+2  A: 

I've written an article with Stefan's help that explains how to do this:

Experiments in Wackiness: Allowing percents, angle-brackets, and other naughty things in the ASP.NET/IIS Request URL

Scott Hanselman