views:

444

answers:

2

In my asp.net mvc app I want to check if a certain url returns a valid response. Therefor I send the url to a method that tests the HttpWebRequest.GetResponse()

On my dev server (vs2008) it works just fine. When deployed on production server however, it returns a Bad Request. The method is never hit and my asp.net custom error pages are not used.

any ideas?

A: 

Perhaps you are running into the ValidateRequest feature of ASP.NET? You can disable this for the page in question by modifying the first line in the aspx file to have the ValidateRequest="false" parameter.

Example:

<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>

You probably don't want to leave it in that state though, as the request validation helps prevent XSS attacks. Using HttpUtility.UrlEncode may also be worth trying, if you aren't already.

J c
+1  A: 

For those who ever read this in the future: I remove the http:// part and used HttpUtility.UrlPathEncode to encode the path and put a route up that takes the last part as {*uri}. This enables me to use a / in the parameter.

Then the checkUri I recreate the uri by adding the http:// and then check if the Host of this uri is the host I expected (in my example the request.url.host).

Then I make the httpwebrequest with the Uri I created and check the request for a response.

borisCallens
Just what I needed thanks!
Webjedi
Seeing how it helped you out too, I figured I could accept my own response as answer then.
borisCallens