Several methods for compiling backend server programs using Asp.net in ExtJS

Source: Internet
Author: User

Currently, most Extjs programs use Java to write backend server programs. The following describes how to use Asp.net to write backend server programs.
There are several ways to write Extjs background server programs using Asp.net:
1. use. net Framework 3.5, because. net 3.5 added an Attributes in WCF to support this method, but it has never been used. If you are interested, please take a look.
2. Define a. aspx page in Asp.net, and then add a method in aspx. CSTO the corresponding Extjs web request. The specific method is as follows:
2.1:
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
ExtJsRequest ();
}
}
Public string ExtJsRequest ()
{

String sRtn = string. Empty;
String sReq = Request ["req"]. ToString ();
SRtn = "{'success': true, 'message':" + sReq + "}";

Return sRtn;
}
}
This method is relatively simple to use, but will generate many unnecessary aspx pages on the Server side. Basically, every method defined requires a page defined on the Server side, which is not conducive to code maintenance.
3: Based on the Servlet principle in Java, Extjs requests are distributed to specific classes on the Server to avoid unnecessary aspx page generation. The specific implementation is as follows:
3.1: implements a class on the Server that inherits the IHttpHandler interface and distributes Extjs requests.
Public class Servlet: System. Web. IHttpHandler
{
Public DataSet ds;
Public Servlet ()
{
Ds = new DataSet ();
String path = HttpContext. Current. Server. MapPath ("../Config. xml ");
Ds. ReadXml (path );
}
Public void ProcessRequest (HttpContext ctx)
{
Try
{
String name = ctx. Request. Url. AbsolutePath;
Name = name. Substring (0, name. IndexOf ('.'));
DataRow [] dc = ds. Tables [0]. Select ("url = '" + name + "'");
If (dc. Length> 0)
{
Type ht = Type. GetType (dc [0] ["Class"]. ToString ());
Object obj = Activator. CreateInstance (ht );
MethodInfo mi = ht. GetMethod (dc [0] ["function"]. ToString ());
Object objRtn = mi. Invoke (obj, new object [] {ctx. Request });
Ctx. Response. Write (objRtn as string );
}
}
Catch (Exception ex)
{
Throw ex;
}
}
Public bool IsReusable
{
Get
{
Return true;
}
}
}
The yellow part of the code is a custom configuration file format.
3.2: custom configuration file
<? Xml version = "1.0" encoding = "UTF-8"?>
<ConfigNode>
<Mapping url = "/YouProject/User/List" Class = "WebUI. Bll. User" Function = "List"> </Mapping>
</ConfigNode>
Related Properties in the configuration file:
URL: the address of the ExtJs request;
Class: the specific Class of the Server to which the request is distributed;
Function: The Extjs Request Method in the Server class;
Code marked as red is a specific method that uses reflection to locate specific classes on the Server;
3.3: define related entity classes
WebUI. Bll. User is defined as follows:
Namespace WebUI. Bll
{
Public class User
{
Public string List (HttpRequest request)
{
... Specific logic
}
}
}
Note that:
1. The WebUI. Bll. User method must be defined as public;
2. The List method must contain an HttpRequest parameter because it needs to obtain the corresponding Extjs request parameter;
3.4: Configure in WebConfig
<HttpHandlers>
<Remove verb = "*" path = "*. asmx"/>
<Add verb = "*" path = "/YouProject/login. aspx" type = "System. Web. UI. PageHandlerFactory"/> --- do not intercept
<Add verb = "*" path = "*. aspx" type = "Servlet"/> --- use custom Httphander for processing
<Add verb = "*" path = "*. asmx "validate =" false "type =" System. web. script. services. scriptHandlerFactory, System. web. extensions, Version = 1.0.61025.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 "/>
<Add verb = "*" path = "* _ AppService. axd "validate =" false "type =" System. web. script. services. scriptHandlerFactory, System. web. extensions, Version = 1.0.61025.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 "/>
<Add verb = "GET, HEAD" path = "ScriptResource. axd "type =" System. web. handlers. scriptResourceHandler, System. web. extensions, Version = 1.0.61025.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 "validate =" false "/>
</HttpHandlers>
Because Asp.net will. the aspx page uses the default HttpHandler to intercept: System. web. UI. pageHandlerFactory, so if you do not want to intercept some pages in your program, such as: login. aspx, logout. aspx, Index. aspx, you can configure it in the By using the 3rd methods, code can be well organized on the Server side, and the entity class can be defined in the Bussiness Layer without adding unnecessary. aspx pages.
By niemeiquan

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.