Two implementation details of restful enorients in ASP. NET MVC

Source: Internet
Author: User

What is rest? Article.ArticleAlthough it is very short, I almost want to despise the degree of the author, but carefully read it, it is indeed found that this article is really useful.

As a person who wants to implement pure restful services as much as possible (or can be referred to as a restful primitive), he hopes that the services can be defined as restful principles as much as possible, if an RPC + Roa (Resource-Oriented Architecture, which is defined in "Restful Web Services Chinese edition") service is created, it is better to use WCF directly, rome was not built in one day, and it is impossible for me to build a pure restful service on the first hand, but it is necessary to move closer to the standard as much as possible.

Some concepts are mentioned in this article, and I soon found that there are two non-conformities in my practice.

1. "Restful web services use methods in http: Get, post, delete, and put. You do not need to specify this method using the URL or request content ."

In my current practice, I use a URL like this: SignatureProgramThe website, since it is for the program, everyone strictly respect the agreement is a very important thing, otherwise you open a resource, such as: http://www.anywhere.com/blog, (In fact, this resource can even be called an API.) How can someone else know whether it is called by get or search? However, using the unified interface principle, we naturally think of using get (HTTP Method) to obtain data, which greatly simplifies the definition of metadata. Back to my application, for log service, http://www.cleversoft.com/svc/log/has been very clearCodeWhen the Log Service (resource representation) is used, the POST method indicates creating a log record. Adding a create statement is superfluous, isn't it returned to the RPC method?

According to the route rules of ASP. net mvc, the simplest and most straightforward method is to create

 
[Httppost] public actionresult index (){//...}

.. this seems to be a bit too much. Obviously a create method uses index .. the client is so cool that it is time for the server to swear by the mother. It is better to continue using the create method name, and the code readability is paramount. Then we can only find a solution from the URL routing, and soon we found a type of httpmethodconstraint. Haha, it looks like it was prepared for this, so we added a new instance for URL routing, the following code is used:

 Public override void registerarea (arearegistrationcontext context) {context. maproute ("svc_default_create", "svc/{controller}/{action}", new {Action = "CREATE "}, new {httpmethod = new httpmethodconstraint ("Post")}); context. maproute ("svc_default_update", "svc/{controller}/{action}", new {Action = "Update "}, new {httpmethod = new httpmethodconstraint ("put")}); context. maproute ("svc_default_delete", "svc/{controller}/{action}", new {Action = "delete "}, new {httpmethod = new httpmethodconstraint ("delete")}); context. maproute ("svc_default", "svc/{controller}/{action}/{ID}", new {Action = "Index", id = urlparameter. optional}) ;}

Delete create from the client URL and use http://www.cleversoft.com/svc/logtaillogrecord!

Let's talk about the second. The second one is relatively simpler. This article mentions that "Restful web services use HTTP status codes as return values .", I think many of the services in Google return the objects created (modified). I don't want to do this. I don't know what I sent to the server, this is a waste, so I returned the new emptyresult (). Of course, the returned emptyresult should be a 200 (HTTP statuscode), but for create, update, for Delete, 200 is returned, which seems to be less professional. Therefore, I made a slight change to httpstatusresult and added several extension methods for the Controller:

 Public static httpstatusresult getresult200 (this controller, Params string [] content) {return New httpstatusresult (httpstatuscode. OK, content. length> 0? Content [0]: "OK");} public static httpstatusresult getresult201 (this controller, Params string [] content) {return New httpstatusresult (httpstatuscode. created, content. length> 0? Content [0]: "created");} public static httpstatusresult getresult202 (this controller, Params string [] content) {return New httpstatusresult (httpstatuscode. accepted, content. length> 0? Content [0]: "accepted");} public static httpstatusresult getresult204 (this controller, Params string [] content) {return New httpstatusresult (httpstatuscode. nocontent, content. length> 0? Content [0]: "No content") ;}

In this way, the operation results can be truthfully reflected to the client as needed. In the future, if someone calls my service from another client, it won't scold me for being NC.

Example:

 
[Httppost] [serviceerror] public actionresult create ([atomentryparameterconvert (typeof (logentry)] logrecord log) {var logmng = new logmanager (); logmng. createlog (log); return this. getresult201 ();}
PS: I heard that. Net 5.0 may have extended attributes? If so, it will not make this operation so ugly. We look forward...
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.