Action Results in MVC
The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers.
HTTP works as a request-response protocol between a client and server.
Four commonly used methods are
- [HttpGet] GET is used to request data from a specified resource.
- [HttpPost] POST is used to send data to a server to create a resource
- [HttpDelete] Delete method is used to delete server data
- [HttpPut] Put method is used to update server data
Action result (Return Types)
Common HTTP Status Codes
Every HTTP transaction has a status code sent back by the server to define how the server handled the transaction. Here is a list of the most common ones.
HTTP Status Code – 200 OK
The request has succeeded. The information returned with the response is dependent on the method used in the request.
HTTP Status Code – 300 Multiple Choices
The requested resource has different choices and cannot be resolved into one. For example, there may be several index.html pages
HTTP Status Code – 301 Moved Permanently
The requested resource has been assigned a new permanent URI and any future references to this resource should use one of the returned URIs.
HTTP Status Code – 302 Found
The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests.
HTTP Status 403 – Forbidden
You can encounter the 403 Forbidden error page when the server understands the client’s request clearly, but for some reasons refuses to fulfil it.
HTTP Status 404 – Not Found
404 is the most well-known HTTP status code out there when the server doesn’t find anything on the requested location. The browser returns a 404 HTML page .
Exception handling -Try-Catch
Exception handling is the process of responding to the occurrence of exceptional conditions requiring special processing. In ASP.NET we can handle exceptions in the following two ways:
- Try-catch-finally block at method level
- Using Application_Error
Change the Index as in the following. Here we are generating an exception at “c = a / b;” inside the try block (next slide).
In the catch we are retuning a different view “Error”. If you run the application you will get an Error page in the browser. In an actual project we can also use the catch block to log the error information.