Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting message from exception thrown by an ASP.NET MVC controller action called from jQuery ajax
    primarykey
    data
    text
    <p>I am trying to secure a controller action that is being called with the jQuery ajax function.</p> <p>In my action method I have the code surrounded by a try-catch block, so I capture all my exceptions and return a JSON result with the exception message. To that point everything works great.</p> <p>The problem begins when I throw an exception outside the try-catch block, for example, if the exception is raised inside a filter action attribute. In that case I cannot return a JSON result because the flow stops abruptly.</p> <p>jQuery catches that exception in the error callback function. But the only place where I have managed to see the exception message is in the xhr.responseText, but it contains the whole "yellow page of death" error from asp.net.</p> <p><em>The very ugly and hackish solution I have used to get the exception message is to extract the text between the &lt;title&gt; tag. But I really hope there is a better way to do this!!</em></p> <p>What would you do in this scenario? How would you secure your ajax actions without writing that logic inside the action method? <strong>How would you show to the user the message from an unhandled exception thrown by an ASP.NET MVC controller action called from jQuery ajax?</strong></p> <p><strong>jQuery Ajax Call:</strong></p> <pre><code>$.ajax({ type: "POST", contentType: "application/json; charset=utf-8", dataType: "json", url: url + "?id=" + id, success: function(data) { if(data.success){ alert('Success'); } else { alert('Fail: ' + data.message); }, error: function(xhr, status, err) { // There has to be a better way to do this!! var title = xhr.responseText.split("&lt;title&gt;")[1].split("&lt;/title&gt;")[0]; alert(title); } }); </code></pre> <p><strong>Controller Action:</strong></p> <pre><code>[MyAttribute] public ActionResult MyAction(int id) { try { // Do something return Json(new { success = true }, JsonRequestBehavior.AllowGet); } catch (Exception exception) { return Json(new { success = false, message = exception.Message }, JsonRequestBehavior.AllowGet); } } </code></pre> <p><strong>Action Filter Attribute:</strong></p> <pre><code>public override void OnActionExecuting(ActionExecutingContext filterContext) { // This causes a 401 Unauthorized HTTP error. throw new UnauthorizedAccessException("Access Denied."); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload