Tuesday, May 1, 2012

Correct way to handle Ajax calls in ASP.Net MVC 3


When coding up Ajax calls in ASP.Net MVC we have a lot of options as far as issuing calls, handling them on the server, and dealing with successes and failures on the client. Some things clearly have a correct answer, but I've been unable to find clear-cut guidance. So, end-to-end, what is the correct way to make an ajax call?



Including



  • What is the best way to inject the url of the action for the url into the ajax call?

  • What are the considerations when choosing JsonBehavior?

  • What is the best way to handle errors on the server side?

    • Should the client side error() callback be triggered by any errors (ie unexpected OutOfMemoryException), or only by foreseeable errors (ie invalid input)?

    • What is the best way of exporting errors in a way that the error() callback will be triggered.

    • Best way of ensuring the error callback gets the correct status code and response text.

    • Should validation errors result in an Error StatusCode or should they be part of a responding validation object.



  • What is the best way to handle errors on the client side?

    • Should unexpected errors server-side be displayed in a similar way as the validation summary? Maybe a just 'something-went-wrong' dialog? Should the client be able to distinguish between the two?





Any other things people have strong opinions on?


Source: Tips4all

No comments:

Post a Comment