'A potentially dangerous Request.Form' - get the message - asp.net

I have a website and someone (every time the same) is trying to send me a message through a textbox (he adds some html code where he shouldn't) and the error is raised.
Unfortunately, all I can get are messages like this one ="...chemistry http://cra..." so it`s no way that I can understand what he try to tell me.
My question is: how I can expand that text characters length limit or handle my own error so I can get the whole message?

In your Global.asax, put an Application_Error event handler in to catch all errors that occur, which should include this one.
Sub Application_Error(object sender, EventArgs e)
{
var ex = Server.GetLastError();
if (ex != null)
//Log it
}

Related

How to catch entitydatasource exception

I have a gridview that is bound to a entitydatasource.I've creaetd this using drag and drop from the asp.net controls in the toolbox, and using an entity data model.I have had little input in the codebehind. For testing purposes I have edited the gridview and added data that is invalid. I've then clicked update to cause an exception.
So my question is I would like to try and catch the exception in my own error handler but I don't know where or how I can do this as I'm not sure which event I should be focusing on. I would just like to know where to begin with this.
Many thanks
You can trap the exception in the OnUpdated event of the EntityDataSource:
protected void EntityDataSource1_OnUpdated(object sender, EntityDataSourceChangedEventArgs e)
{
if (e.Exception != null)
{
// handle here
e.ExceptionHandled = true;
}
}
}
you won't able to given the form was designed using drag'n drop, declarative syntax. You are better off validating the user input before submitting it to the server. This should catch most exceptions.
your other option is to replace the declarative markup with code in the code behind where you can catch exceptions or call validation prior to calling SaveChanges();
you can catch the exception in global.asax but it will show generic error.

Application_OnError not always working

I experience strange problem. We have error handling in global.asax that would redirect user on special page in case if error happened:
void Application_Error(object sender, EventArgs e)
{
.......
string pageError = "~/LastError.aspx?AfterNextClick=" + afterNextClick.ToString();
if (Request["guid"] != null)
pageError += "&guid=" + Request["guid"];
Server.Transfer(pageError);
}
Custom errors are turned off.
<customErrors mode="Off"/>
Most of the times the Application_OnError works perfectly and redirects users to the specific page, but sometimes, users are not redirected anywhere and an ASP.NET exception page is displayed.
So is there any situations in which Application_OnError in global.asax wouldn't fire?
Probably an exception is ocurring inside the Apllication_Error method. Take a closer look at the code you place in this method (where you placed ...... ).
Try placing a try/catch block in this method to debug what's going on...

Response.Redirect() ThreadAbortException Bubbling Too High Intermittently

I understand (now) that Response.Redirect() and Response.End() throw a ThreadAbortException as an expensive way of killing the current processing thread to emulate the behaviour of ASP Classic's Response.End() and Response.Redirect methods.
However.
It seems intermittently in our application that the exception bubbles too high. For example, we have a page that is called from client side javascript to return a small string to display in a page.
protected void Page_Load(object sender, EventArgs e)
{
// Work out some stuff.
Response.Write(stuff);
Response.End();
}
This generally works, but sometimes, we get the exception bubbling up to the UI layer and get part of the exception text displayed in the page.
Similarly, else where we have:
// check the login is still valid:
if(!loggedin) {
Response.Redirect("login.aspx");
}
In some cases, the user is redirected to login.aspx, in others, the user gets an ASP.NET error page and stack dump (because of how our dev servers are configured).
i.e. in some cases, response.redirect throws an exception all the way up INSTEAD of doing a redirect. Why? How do we stop this?
Have you tried overloading the default Redirect method and not ending the response?
if(!loggedin) {
Response.Redirect("login.aspx", false);
}
You can use the following best-practice code instead, as explained by this answer to prevent the exception from happening in the first place:
Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();
Since I was looking for an answer to this question too, I am posting what seams to me a complete solution, rounding up the two above answers:
public static void Redirect(this TemplateControl control, bool ignoreIfInvisible = true)
{
Page page = control.Page;
if (!ignoreIfInvisible || page.Visible)
{
// Sets the page for redirect, but does not abort.
page.Response.Redirect(url, false);
// Causes ASP.NET to bypass all events and filtering in the HTTP pipeline
// chain of execution and directly execute the EndRequest event.
HttpContext.Current.ApplicationInstance.CompleteRequest();
// By setting this to false, we flag that a redirect is set,
// and to not render the page contents.
page.Visible = false;
}
}
Source:
http://www.codeproject.com/Tips/561490/ASP-NET-Response-Redirect-without-ThreadAbortExcep

How do I crash the App Pool?

Our ASP.NET 2 web application handles exceptions very elegantly. We catch exceptions in Global ASAX in Application_Error. From there we log the exception and we show a friendly message to the user.
However, this morning we deployed the latest version of our site. It ran ok for half an hour, but then the App Pool crashed. The site did not come back up until we restored the previous release.
How can I make the app pool crash and skip the normal exception handler? I'm trying to replicate this problem, but with no luck so far.
Update: we found the solution. One of our pages was screenscraping another page. But the URL was configured incorrectly and the page ended up screenscraping itself infinitely, thus causing a stack overflow exception.
The most common error that I have see and "pool crash" is the loop call.
public string sMyText
{
get {return sMyText;}
set {sMyText = value;}
}
Just call the sMyText...
In order to do this, all you need to do is throw any exception (without handling it of course) from outside the context of a request.
For instance, some exception raised on another thread should do it:
protected void Page_Load(object sender, EventArgs e)
{
// Create a thread to throw an exception
var thread = new Thread(() => { throw new ArgumentException(); });
// Start the thread to throw the exception
thread.Start();
// Wait a short while to give the thread time to start and throw
Thread.Sleep(50);
}
More information can be found here in the MS Knowledge Base
Aristos' answer is good. I've also seen it done with a stupid override in the Page life cycle too when someone change the overriden method from OnInit to OnLoad without changing the base call so it recursed round in cirlces through the life cycle: i.e.
protected override void OnLoad(EventArgs e)
{
//some other most likely rubbish code
base.OnInit(e);
}
You could try throwing a ThreadAbortException.

Page.Validate Null Reference Exception

I have a bit of a problem I was wondering if you could help me.
I have the following little bit of code:
protected void btnSubmit_Click(object sender, EventArgs e)
{
Page.Validate("RadMaterial");
Page.Validate("TopX");
int max = 0;
if (int.TryParse(txtbxHowMany.Text, out max))
{
GridView1.DataSource = this.GetMaterialData("123456",radTopx.SelectedItem.Value, "Primary", max);
GridView1.DataBind();
}
}
I have a couple of validation groups set up the first - if the click is made and the txtbxHowMany is not populated, a simple error is shown.
I also set up a validation group for the radiobutton list so that, should the user hit submit without checking a radiobutton, the required field validation should fire.
However, it is not firing. I am getting a "NullReferenceException was handled by user code."
My thinking is that because the radTopx.SelectedItem.Value is, well, null.
How would I go about getting around this little issue of mine? Again, apologies for what is most likely a ridiculously easy question.
Interesting the way you used Validate method.
See what msdn says about it.
http://msdn.microsoft.com/en-us/library/dwzxc386.aspx

Resources