(original) Object-oriented system docking interface programming. 5th article (end)

Source: Internet
Author: User

Next: http://www.cnblogs.com/mazhiyuan/p/5224054.html

This is the end of this article. The main talk is how to start the call, and how to control must be a GET request or must be a POST request, is how to limit.



For example, we create a news.ashx front-end processing file with a news module








<%@ WebHandler language="C #"class="News"%>usingSystem;usingsystem.web;usingSystem.IO;usingSystem.Web.Script.Serialization;usingZgmz. Model;usingZGMZ.BLL;usingZgmz. UIL. App;usingZgmz. UIL. App.news;usingSystem.Collections.Specialized;usingZgmz. UIL. Log;usingZgmz. Config;/// <summary>///Accept Post Mode transfer/// </summary> Public classnews:ihttphandler{ Public voidProcessRequest (HttpContext context) {stringAction = context. request["Action"]; stringHttpMethod =context.        Request.HttpMethod.ToLower (); if(string. IsNullOrEmpty (Action)) {context. Response.Write ("{\ "codeid\": \ ""+ Code.FailActionIsNull.CodeId +"\ ", \" codedescription\ ": \""+ Code.FailActionIsNull.Description +"\ ", \" errormessage\ ": \" Please pass the action parameter \ "}"); return;        } commandtype type; if(! Enum.tryparse (Action, outtype)) {context. Response.Write ("{\ "codeid\": \ ""+ Code.FailActionIsNotExists.CodeId +"\ ", \" codedescription\ ": \""+ Code.FailActionIsNotExists.Description +"\ ", \" errormessage\ ": \" The action value passed is not registered in the System \ "}"); return; } basecommand Command=factory.appfactory (). Getnewsfacade ().        Createcommandinstance (type); if(HttpMethod = ="Post") {Post post= Command asPost; if(Post = =NULL) {context. Response.Write ("{\ "codeid\": \ ""+ Code.FailHttpMethodError.CodeId +"\ ", \" codedescription\ ": \""+ Code.FailHttpMethodError.Description +"\ ", \" errormessage\ ": \" recommended attempt to change request method \ "}"); return; } post. Input= This.            Getqueryparameters (context); Try{post.            Excute (); }            Catch(System.Exception exp) {context. Response.Write ("{\ "codeid\": \ ""+ Code.FailServer.CodeId +"\ ", \" codedescription\ ": \""+ Code.FailServer.Description +"\ ", \" errormessage\ ": \""+ exp. Message +"\"}"); return; }        }        if(HttpMethod = ="Get") {GetGet= Command asGet; if(Get==NULL) {context. Response.Write ("{\ "codeid\": \ ""+ Code.FailHttpMethodError.CodeId +"\ ", \" codedescription\ ": \""+ Code.FailHttpMethodError.Description +"\ ", \" errormessage\ ": \" recommended attempt to change request method \ "}"); return; }            Get. Input =context.            Request.params; Try            {                Get.            Excute (); }            Catch(System.Exception exp) {context. Response.Write ("{\ "codeid\": \ ""+ Code.FailServer.CodeId +"\ ", \" codedescription\ ": \""+ Code.FailServer.Description +"\ ", \" errormessage\ ": \""+ exp. Message +"\"}"); return; }        }        //logabstract log = Logfactory.getfilelog (); //log.        Filelogserverpath = Params.Global.FileLogServerPath; //log. Writelog ("app log", command.) Output);context. Response.Write (Command.    Output); }    /// <summary>    ///take the transmitted parameters ./// </summary>    /// <author>Zhiyuan Ma</author>    Private stringGetqueryparameters (HttpContext context) {Stream Jsondatastream=context.        Request.inputstream; StreamReader Reader=NewStreamReader (Jsondatastream); stringJsondata =Reader.        ReadToEnd (); Reader.        Close (); returnJsondata; }     Public BOOLisreusable {Get        {            return false; }    }}

The code is elaborate:
String action = context.    Request["Action"]; The type of the request. For example, to add news, the action value is the addappnews that was previously spoken. Must be this value to facilitate reflection. The name must be the same as AddAppNews.cs.

String HttpMethod = context.    Request.HttpMethod.ToLower (); Gets the request method. This is the reason why the request must be a get or must be a post.

if (! Enum.tryparse (action, out type) when the action name is inconsistent with the. cs file, an error is given.

Post post = command as Post; Limit the request must be post, if he is a get sent over, then this value post will be null, and then return the error message to the call tune.

Get get = command as get; Again, this code restricts the request to be get. If the post is used, the Get object will be null.

Get.    Excute (); Then began to deal with the relevant business.

Context. Response.Write (Command.    Output); Finally, use command. Output is serialized into a JSON or XML-formatted string that is returned to the caller.

Finally: Let's add the news as an example. The URL of his post request should look like this: Http://www.163.com/News.ashx?action=addappnews

If it is a news feed, then the URL of his GET request is this: http://www.163.com/news.ashx?action=getappnewsbyuserid&userid=1

When you want to see this, there are 4 more: Here are the links:

(original) Multi-system needs docking, I wrote an interface framework. Practicality is very strong, write to everyone to communicate. Need to be able to move directly past use. (1th article) http://www.cnblogs.com/mazhiyuan/p/5224046.html

(original) Object-oriented system docking interface programming. 2nd article http://www.cnblogs.com/mazhiyuan/p/5224049.html

(original) Object-oriented system docking interface programming. 3rd article http://www.cnblogs.com/mazhiyuan/p/5224050.html

(original) Object-oriented system docking interface programming. 4th article http://www.cnblogs.com/mazhiyuan/p/5224054.html

(original) Object-oriented system docking interface programming. 5th (end) http://www.cnblogs.com/mazhiyuan/p/5224056.html

If read, have not understood can comment to me.

It's really good to use. There are students who need to do the interface. The entire frame can be used.

(original) Object-oriented system docking interface programming. 5th article (end)

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.