Category Archives: ASP.NET

web development with asp.net framework

Gestion des erreurs en ASP.NET MVC

Standard

Article publié dans le magazine Programmez ! – Avril 2014 – N° 173

Lors de la conception d’une application web, gérer le comportement du système en cas d’erreur est important et ne devrait pas être laissé au hasard. D’une part, visualiser une page système peu accueillante, non stylisée, et loin des couleurs du site n’est pas toujours agréable pour l’utilisateur qui vient en plus de recevoir une erreur. D’autre part, si certains paramétrages ne sont pas effectués, l’utilisateur pourrait avoir accès aux détails techniques de l’exception. Un visiteur mal intentionné pourrait se servir de ces détails pour accumuler des connaissances sur la structure technique du site et trouver plus facilement une faille de sécurité à exploiter. Ainsi, il appartient à l’équipe de réalisation du site de déterminer en amont le niveau de détails d’erreur à délivrer à l’utilisateur, les codes d’erreur HTTP à renvoyer en fonction des situations et les pages d’erreur qui seront affichées. En somme, il s’agit de décider d’un comportement applicatif en cas d’erreur qui soit uniformément suivi au cours des développements.

ASP.NET MVC offre de nombreuses solutions pour gérer une exception de manière centralisée et rediriger vers une ressource en particulier. Gérer les erreurs peut donc se faire de plusieurs façons et peut s’adapter à divers scénarios. Ces différentes manières seront exposées au cours de cet article, du niveau le plus global à celui le plus fin.

Read the rest of this entry

Calling a server method (asp.net) from the browser

Standard

Sometimes,  you use JQuery within your Asp.Net projects to provide richer and more interactive web experience. The user now can drag and drop, admire your smooth animations, interact in many ways with the page and experience a whole bunch of other funny things. On top of that, you could appreciate how integrating JQuery with ASP.NET projects is pretty straightforward.

Moreover, to get your page to work properly, you sometimes have to  focus on client-sided effects only and to forget about… postbacks ! Your HTML elements then won’t be bound to the server and won’t trigger any postbacks nor be referenced in any aspx class. They will have nothing to do with the viewstate and they won’t notify any data change.  As a consequence, if the user changes the webpage content, some properties or the order of a few elements  through JQuery manipulations, the server won’t be able to retrieve by itself those changes. Let’s stick with our previous example : the dynamic forms (dynamic webforms with JQuery). The user wants to create his own forms. It was all implemented through Javascript and JQuery, adding, dragging, dropping.

>> Could we now save it all to our database thanks to a little Asp.Net method ? which means formatting the data, sending it to the server which will eventually treat it. So how to send back data to  the server ?
>>How to call a server method without the postback mechanism but just with pure raw HTML controls never bound to the server ?
Of course, a simple Ajax call will do the trick !
We’ll see how  to :

  1. build this simple Ajax call to the asp.net server
  2. how to format basic data with JSON
  3. how to format more complex data like serializing and deserializing Javascript-.NET object, even with inheritance. Read the rest of this entry

Downloading a file within an asp updatepanel

Standard

In this article, we’ll see how to make full page postback controls work in an update panel.
We will then raise the problem of a button field in a gridview cell in an update panel that requires full page postback registration and suggest a workaround.

How basically the update panel works ?

The asp update panel is a native control which performs an asynchronous postback to render selected parts of the webpage. If you already know ajax mechanisms, update panel shouldn’t be mysterious for you. It deals indeed with Ajax and partial-page update to allow richer and more ergonomic web application.

Thus, unlike with a full page postback, a partial page postback sends a request back to the server through client-side script. Only the html markup inside the update panel is updated through Javascript again which deals with the server response to inject the new html content inside the update panel. The update panel waits for the server to return a special snippet of html code in a certain format. While the async request is sent out, update panels wait for this specifically formatted response.

Why my upload or download file function won’t work within an update panel ?

All of the controls placed inside the update panel are converted into partial postback controls. That’s pretty coherent because we want the page to be fast and user-friendly so the nested controls won’t raise a full reload of the page.  But those controls will go on working as long as they don’t need full postback.

But some controls might require full page postback to work, in other words they demand a classic and complete HTTP response to write into, to flush and to end. They won’t stand an async postback with a partial http markup response to inject in the page through JavaScript. What do you need to do then ?

Read the rest of this entry