Implementation of simple MVC

Source: Internet
Author: User

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 ...)

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.