Solutions to several problems in the development of HTML+ASHX

Source: Internet
Author: User
Tags constructor reflection variable scope

The garden friends who are dealing with html+ashx will surely find that this model is graceful, but it will encounter some difficult places in the development. I am no exception, the following is the actual development of their own summed up a few of the experience, I hope to share with you, more hope that everyone's suggestions and better solutions!

Question 1: Replace Switch...case with a commissioned dictionary.

This problem is found in the processing of the request, we certainly do not want to build a lot of handler in their own projects to deal with so many requests, so the idea of processing multiple requests in a handler, Ajax requests are added an action parameter, In handler according to this action to do the corresponding processing or return the corresponding data, here certainly no one uses If...else to judge the action, most people will think with Switch...case, at the beginning I also use the switch, but gradually found that Each case is not like a block of code and cannot provide a separate scope for the variables in it! With the Dragon beads in the words of the Monkey King "really nerve-racking."

Search on the Internet a bit, but also a lot of people encounter this problem. One solution is to put each processing individually into a handler method, which is clear, but in the ProcessRequest method, the corresponding method is invoked with reflection! I was not satisfied with this solution, so I thought of the delegate, think of the dictionary, the reflection call method into the dictionary index delegate.

First, declare a private, static delegate dictionary in the handler:

Static dictionary<string, func<string>> HS;

The HS is then initialized with a static constructor in handler (the class of the generic handler), and more importantly, to add the processing method to the static constructor:

Static Handler () 
{
hs = new dictionary<string, func<string>> ();
Hs. Add ("Add", delegate ()
{
int id = Int.      Parse (req ("id"));
string title = Req ("title");
return "Add";
});
Hs. ADD ("Update", delegate ()
{
int id = Int.      Parse (req ("id"));
string title = Req ("title");
return "Update";
});
}

Finally, it is called in the ProcessRequest method:

Context. Response.ContentType = "Text/plain";
HttpRequest req = context. Request;
String action = req["action". ToLower ();
string result = Hs[action] ();
Context. Response.Write (Result);

This avoids the problem of switch...case variable scope and the efficiency of reflection. With regard to the Req () method used above, my idea is to provide public things in a static way, such as:

static string req (String key)
{return
httpcontext.current.request[key];
static string Jss (Object obj)
{
JavaScriptSerializer JSS = new JavaScriptSerializer ();
Return JSS.  Serialize (obj);
}

Question 2: Permission issues.

You're not going to want it. Data can continue to be accessed after the user has not logged in or has expired. This assumes that the user logged in session["user"] to store, of course, in handler to Judge session["user" is very simple things, but the question is how you let session["user" is null when users jump to the specified page ( Here the assumption is landing page login.html). Haha, then you will not think of using the context. Response.Redirect ("login.html") such a word to solve it! My first reflection is this, but the analysis is that Ajax is requesting data, This is to allow Ajax to request login.html This page, the results should be login.html source code, analysis is such analysis, can still not give up, or test a bit, the results as analyzed, login.html source code as the result of the AJAX request returned!

In fact, we understand that there is a very simple way to return a particular value when session["user" is null, assuming "Unlogin", and then determining whether the return value is "Unlogin" after each Ajax request completes.

This method is very simple, but also very reliable, but very stupid, very troublesome, the feasibility is not high. Then I think of Jquery.ajaxsuccess (), want to use it to do unified processing, I think of it when I am a little worried, jquery will be the first call to the specific request of the callback function and then call this global callback function? I carried out the test with this question, and as expected, execute the callback of the specific request and then perform the global callback! Did not bring to justice, had to check jquery's source code ~. In the uncompressed Jquery-1.4.2.js found success () This method, sure enough, after the change in the order of the following:

Function success () {  
if (s.global) {
trigger ("ajaxsuccess", [XHR, S]);            
/If a local callback is specified, fire it and pass it the data
If (s.success && xhr.respons    etext!= "Unlogin") {
S.success.call (callbackcontext, data, status, XHR);
}
}

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.