典型的方式做,這是創建一個包含另一大類「肉'的查詢代碼,然後在兩個地方調用它;
// in QueryService.cs, you define a library for querying...
public class QueryService
{
public DataTable PerformQuery(string searchTerm)
{
// your query logic goes here. Return a logical result like a DataTable, some JSON, etc.
}
}
// in Query.ashx, call your query service;
public void ProcessRequest(HttpContext context)
{
var searchTerm = context.Request.QueryString["query"];
var results = new QueryService().PerformQuery(searchTerm);
context.Response.Write(...results...);
}
// in Start.ashx, call your query service again;
public void ProcessRequest(HttpContext context)
{
var searchTerm = context.QueryString["homepage"];
var results = new QueryService().PerformQuery(searchTerm);
context.Response.Write(...results...);
}
這樣,你的應用程序可以執行查詢,用少量代碼,而無需進行其他Web請求這是昂貴的,通過使標準的函數調用的任何部分。
爲什麼你需要提出網絡請求?如果它在同一個項目中,則調用相同的應用程序代碼。 –