The opening speech has nothing to do with this topic. I highly respected a mentor who hasn't seen anyone for several days. Today I heard that it was a disease and people have also appeared. Wish him good health and longevity!
When using ASP. net mvc to build a restful service, the following question occurs: How does the server return a response when the post, put, and delete methods are used to send a request? If an exception occurs during the operation, how can I notify the client?
With this problem, I tried to build an actionresult derived class:
Namespace system. web. MVC {public class httpstatusresult: actionresult {public httpstatusresult (): This (httpstatuscode. OK) {} public httpstatusresult (httpstatuscode statuscode): This (statuscode, "") {} public httpstatusresult (httpstatuscode statuscode, string content) {This. statuscode = statuscode; this. content = content;} Public String content {Get; set;} public httpstatuscode statuscode {Get; set;} public override void executeresult (controllercontext context) {context. httpcontext. response. statuscode = (INT) statuscode; context. httpcontext. response. contenttype = "text/XML, charset = \" UTF-8 \ ""; if (string. isnullorempty (context. httpcontext. request. headers ["Accept-encoding"]) = false) {string acceptencoding = context. httpcontext. request. headers ["Accept-encoding"]. tolower (); If (AC Ceptencoding. contains ("gzip") {context. httpcontext. response. filter = new gzipstream (context. httpcontext. response. filter, compressionmode. compress); context. httpcontext. response. appendheader ("content-encoding", "gzip") ;}} using (VAR writer = xmlwriter. create (context. httpcontext. response. output) {This. responsecontent. writeto (writer) ;}} private xmldocument responsecontent {get {xmldocument Xml = new xmldocument (); XML. loadxml (string. Format ("<? XML version = \ "1.0 \"?> <Response xmlns = \ "http://schema.cleversoft.com/webutil/httpstatusresult/1.0/\"> <content >{0 }</content> </response> ", this. Content); Return XML ;}}}}
This class can be used to return an HTTP status message to the requesting client. If the operation is normal, 200,202,204 is used as needed. What is the situation? According to HTTP/1.1:
Post:
The action was med by the post method might not result in a resource that can be identified by a URI. in this case, either 200 (OK) or 204 (NO content) is the appropriate response status, depending on whether or not the response between des an entity that describes the result.
If a resource has been created on the origin server, the response shocould be 201 (created) and contain an entity which describes the status of the request and refers to the new resource, and a location header.
Put:
If a new resource is created, the origin server must inform the user agent via the 201 (created) response. if an existing resource is modified, either the 200 (OK) or 204 (NO content) Response codes shoshould be sent to indicate successful completion of the request.
Delete:
A successful response shocould be 200 (OK) if the response provided des an entity describing the status, 202 (accepted) if the action has not yet been enacted, or 204 (NO content) if the action has been enacted but the response does not include an entity.
All in all, let's see what you actually did and return an HTTP status code that matches it. In this process, we may also encounter the need to use the 3xx system, but there is no such complicated plan in the service I Want To Do. According to the previous study, it is found that Google's 3xx service is mostly used and the whole process is complicated.
The above is only a response to the success of sending post, put, and delete. For this reason alone, it seems that there is no need to do the httpstatusresult type. To implement restful services by yourself, exception handling must be considered. In my current ideas, if some operations encounter server errors, you must use the httpstatusresult type to pass the error message to the client, that is, the httpstatusresult. content attribute. The usage is as follows:
Return new httpstatusresult (system. net. httpstatuscode. internalservererror, "exception message .");
I used internalservererror (500) when sending an exception ).
The problem is that after 500 is used, the client does not know that this is your "deliberate" issue. It only knows that the server has encountered an error and then in webrequest. when getresponse () is used, an exception is thrown directly, and no webresponse is returned. Why not use the exception information returned by content? Fortunately, M $ is not so stupid. You can take a closer look at the definition of webexception and you will find that webexception was used in the past. response can get the response returned by the server just now, of course, webexception. the status must not be connectfailure (value: 2), and the response attribute must not be empty (nonsense). Next, you can use xmlreader to get data from responsestream, the data structure can be defined according to your preferences :). Here, we recommend that you define a namespace in the response XML. why Nia? In this way, you can determine that this is your own, rather than the response from others.
Finally, let's go to the HTTP Status Code definition:
100 continue
101 switching protocols
102 Processing
200 OK
201 created
202 accepted
203 non-authoritative information
204 NO content
205 reset content
206 partial content
207 multi-status
226 im used
300 multiple choices
301 moved permanently
302 found
303 see other
304 not modified
305 use proxy
306 (unused)
307 temporary redirect
400 bad request
401 unauthorized
402 payment required
403 Forbidden
404 not found
405 method not allowed
406 not acceptable
407 proxy authentication required
408 request timeout
409 conflict
410 gone
411 length required
412 precondition failed
413 Request Entity too large
414 request-URI Too Long
415 unsupported media type
416 requested range not satisfiable
417 expectation failed
418 I'm a teapot
422 unprocessable entity
423 locked
424 failed dependency
425 (unordered collection)
426 upgrade required
500 Internal Server Error
501 not implemented
502 Bad Gateway
503 service unavailable
504 gateway timeout
505 HTTP Version Not Supported
506 variant also negotiates
507 insufficient storage
510 not extended