问题
I recently implemented ELMAH on my site and I have noticed that we frequently get the "Invalid character in a Base-64 string" error. I have never triggered it myself and none of our users have complained about it so I don't know what is going on. From the little I was able to find about it it seems like the viewstate might be getting too big or becoming corrupted or something. Does anybody know what causes this and how to prevent it? Here are what I believe are the pertinent lines in the YSOD.
[FormatException: Invalid character in a Base-64 string.]
[ViewStateException: Invalid viewstate.
[HttpException (0x80004005): The client disconnected.]
Is there anything I can do about these errors or should I just filter them in ELMAH?
回答1:
It might be how things are configured. Take a look at this:
http://groups.google.com/group/elmah/browse_thread/thread/ec9c4bdddaa1a9e/9108b48d3def87db?lnk=gst&q=viewstate+elmah#9108b48d3def87db
UPDATE
Try and identify where it is occurring. There may be several potential causes:
"Invalid Character in Base-64 String" using ASP.NET and C#
asp.net Invalid character in a Base-64 string
At the end of the day, if as you say, it is not causing any problem in production, then you can filter out these errors. Try setting EnableViewStateMac to false?
回答2:
In my experience, this error tends to be cause by the user double-clicking on a button that triggers a postback. The second postback request cancels the first. The viewstate of the first request is only partially submitted, so it is invalid, but the error can't be sent to the browser because it has disconnected, which triggers the top-level error. This may be a bigger problem if the user doing something twice causes trouble. Otherwise, these errors can simply be filtered. Here's a good example of filtering out similar errors in ELMAH: https://stackoverflow.com/a/2549509/267448
If you're using ASP.NET WebForms, here's some code to disable the triggering control during postback: http://disturbedbuddha.wordpress.com/2007/12/10/disabling-a-trigger-control-during-asynchronous-postback/
Beware that if you disable an HTML <input type="submit"> button, it is excluded from your form variables, so your server-side Click event doesn't fire. Changing it to an <input type="button"> fixes that. In WebForms, that would be <asp:Button UseSubmitBehavior="False" />.
The above works with WebForms AJAX pages, but here's also a little bit of jQuery I came up with for other pages.
$(function () {
$("a[href^='javascript']").click(function (event) {
if (event.target.disabled)
return false;
event.target.disabled = true;
setTimeout(function () {event.target.disabled = false;}, 250);
});
});
来源:https://stackoverflow.com/questions/4810232/asp-net-invalid-character-in-a-base-64-string