handling validateRequest=true in code behind. is there a workarou

G

Guest

is there a way to avoid the validateRequest error page in my code behind
file? i can't seem to find a way to handle the error w/o an error code or
exception being thrown...

i am NOT looking for a link that explains what it does and what it doesn't
do OR the pros/cons, as i've already read through many of those. i just want
to know if i can code some sort of exception to catch for it so that the ugly
yellow/white microsoft generic page doesn't pop up.

there is no error code provided so i am assuming that the only way to avoid
this page is setting validateRequest=false and coding my own validation. i
DO want to use this feature though...is there such a way?

thanks all.
 
N

Nicole Calinoiu

One approach would be to catch the error from the Application_Error
procedure in your Global.asax file. e.g.:

protected void Application_Error(Object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex is HttpRequestValidationException)
{
// Do one thing.
}
else
{
// Do something else.
}
}

If this isn't a good solution for your purposes, it might help if you could
provide a few details concerning your desired outcome.

HTH,
Nicole
 
G

Guest

HttpRequestValidationException can not be found. I have tried adding
System.Web and System.Runtime.InteropServices;

Any idea what I am missing here? Thanks for your help this seems like it
would fix the problem as soon as I can get past this minor problem.
 
N

Nicole Calinoiu

It's in the System.Web namespace, in the System.Web.dll assembly, but it was
only introduced in version 1.1 of the .NET Framework. If you're using
version 1.0, you won't see it, but you shouldn't be seeing request
validation exceptions either...
 
G

Guest

Hi Nicole,

Definitely have 1.1 installed and "using System.Web" is at the top of the
Global.asax.cs file. HttpRequestValidationException exception still isn't
found. Am I missing some reference (in the System.Web.dll assembly)? Or am
I referencing something incorrectly? I was assuming the using keyword with
System.Web would take care of the HttpRequestValidationException not being
found.

These are my namespaces:
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;

Code for the app error:
protected void Application_Error(Object sender, EventArgs e)
{
// Here we will catch the HttpRequestValidationException
Exception ex = Server.GetLastError();
if (ex is HttpRequestValidationException) {
// Server.Transfer(some page);
}
}

Thanks for the quick replies.
 
N

Nicole Calinoiu

What exactly do you mean by "HttpRequestValidationException exception still
isn't found"? Are you receiving a compile-time error? A runtime error?
Something else entirely?
 
G

Guest

Sorry if I was unclear, it's a compile time error. Right now, I have to
comment the line out to get my website working.

When uncommented this error appears:
Global.asax.cs(48): The type or namespace name
'HttpRequestValidationException' could not be found (are you missing a using
directive or an assembly reference?)

As from the last post, I have included System.Web, but it does not seem to
have a definition for HttpRequestValidationException.
 
N

Nicole Calinoiu

Hmm... It really should work if version 1.1 of the framework is being used.
Do you have any versions other than 1.1 installed on your developement
machine? Are you perhaps using VStudio 2002 to edit the project?
 
G

Guest

Acutally funny you mentioned that...I am using VS.NET to edit the project,
but in the "About..." section it says "Microsoft Development Environment 2002
Version 7.0.9466". It also says Microsoft .NET Framework 1.0 Version
1.0.3705"

But in the control panel it says that I have Microsoft .NET Framework 1.1
installed. Or maybe that has nothing to do with which version VS.NET and
ASP.NET is using?

What will I need to reconfig?
 
N

Nicole Calinoiu

You can have both versions 1.0 and 1.1 of the .NET Framework installed on
the same machine. When both are installed, each web application running on
the machine can be independently configured to point to either version.
However, the VStudio versions map more strictly to the .NET Framework
versions. VStudio 2002 will only compile against framework 1.0, and VStudio
2003 will only compile against framework 1.1. Since you're running VStudio
2002, you will not be able to use it to compile a project that uses 1.1
features.
 
G

Guest

So basically what you're saying is I won't be able to handle this
validateRequest error until i get VS.NET 2003 copy on my machine? :(

Oh no...Well, thanks for all your help. I will see if I can find another
work around for this or I might just have to get VS.NET 2003.
 
N

Nicole Calinoiu

Not necessarily. There are other IDEs (e.g.: WebMatrix,
http://www.asp.net/webmatrix/default.aspx?tabIndex=4&tabId=46) that you
might be able to use to compile your application. You also have the option
of using the command-line compiler (csc.exe) distributed with the .NET
Framwork SDK
(http://www.microsoft.com/downloads/details.aspx?familyid=9b3a2ca6-3647-4070-9f41-a333c6b9181d).
However, if you do plan on developing and maintaining 1.1 applications,
installing an IDE that can handle this properly might be a good idea...
 
N

Nicole Calinoiu

Just had another thought... If this is your only 1.1 problem, you could
potentially work around it by using only the type name in your code. e.g.:

Exception ex = Server.GetLastError();
if (ex.GetType().FullName == "System.Web.HttpRequestValidationException")
{
...

It's a bit kludgy, but it should compile under VStudio 2002. If you go this
route, you should definitely test whether it subsequently runs as expected
under Framework 1.1.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top