We have an MVC.Net 2 application running with .Net 4.0. There are cases where embedded code in our views may err, sending malformed HTML to the browser.
How can I tell Internet Explorer to close any open attributes and elements so it can properly render an error message?
To catch and log errors in the embedded code, I'm enclosing the view's HTML and embedded code in the try block of a try-catch structure and just want a simple message displayed to the user after logging the error.
Example:
<%
try {
%>
<div id="div1">
<label class="<%=embedded.code.that.may.err%>">mylabel</label>
<div id="div2" class="data-style">
<%=more.embedded.code.that.may.err%>
</div>
</div>
<%
}
catch(Exception ex) {
(new LogManager()).Save(ex);
%>
<div style="color:red;font-weight:bold;font-size:large;">
Sorry, there was an error.
</div>
<%
}
%>
Don't. Have your error prone code run and put the result into string variables. Put that in the try-catch block. Then, it there's no error you can write out the entire #div1 block including your already evaluated strings knowing that there won't be an error.