Let's create a simple mvc implementation dedicated to json calls, and finally expose the following C # code to js calls (the code is downloaded at the end ):
Json GetUser( [HttpQueryString(= = = ==
The classes used above, such as Json and HttpQueryString, are self-written, not asp.net mvc classes.
(HttpQueryString ("x_user") indicates that the modified variable userId is obtained from the QueryString collection and the key is x_user. How can this problem be easily understood)
Add the corresponding HandlerFactory to the configuration file (indicating that all paths conforming to ajax/*. ashx will be processed by MvcHandlerFactory. Currently, only JsonHttpHandler is implemented ):
If the entry is found, it is easy to do. Let's take a look at this MvcHandlerFactory:
IHttpHandler GetHandler(HttpContext context, requestType, url, = (controllerDescriptor == Exception(= (methodDescriptor == Exception(=
Complete the following tasks:
- Parse the target Controller according to the context request
- Parse the target function Method based on the target Controller and context request.
- During Method parsing, the parameters and value locations involved in this Method are also parsed internally (from Request. form, Request. queryString, Cookie, Headers, or Any (Request. params), key, etc.
Let's take a look at JsonHttpHandler:
JavaScriptSerializer jsonSerializer = .controllerDescriptor =.methodDescriptor = { { = .controllerDescriptor.ControllerType.GetMethod(<> parameters = List<> ( pd value = parameters.Add(value); } result=mi.Invoke( json= context.Response.Write(json); } }
You also need to add the class with the suffix of searching All controllers in global to register the Dictionary. Here is some traversal. You can read the code and the code will not be pasted.
Currently, this framework is simple, but we can see many points that can be inserted, such as [RequestLimit (HttpRequestType. Post.
Once you can go deep into the framework, many problems are no longer difficult and can be solved quickly.
The code written by vs2012 is attached (the home computer only has vs2012, and there is no vs2010 ...)