但对我们的需求来说,其实也非常的简单,既然他硬编码了,那我们再派生一下,把这些方法override一下不就可以了:
1 /// <summary> 2 /// Provides a simple implementation of the System.Web.HttpWorkerRequest abstract class that can be used to host ASP.NET applications outside an Internet Information Services (IIS) application. 3 /// This class can be used for unit test which needs a web host. 4 /// </summary> 5 public class TestWorkerRequest : SimpleWorkerRequest 6 { 7 private readonly string hostName = ""; 8 9 /// <summary> 10 /// Initializes a new instance of the <see cref="T:System.Web.Hosting.SimpleWorkerRequest"/> class when the target application domain has been created using the <see cref="M:System.Web.Hosting.ApplicationHost.CreateApplicationHost(System.Type,System.String,System.String)"/> method. 11 /// </summary> 12 /// <param name="page">The page to be requested (or the virtual path to the page, relative to the application directory).</param> 13 /// <param name="query">The text of the query string.</param> 14 /// <param name="output">A <see cref="T:System.IO.TextWriter"/> that captures output from the response</param> 15 /// <param name="hostName">The host name that will be requested.</param> 16 public TestWorkerRequest(string page, string query, TextWriter output, string hostName) 17 : base(page, query, output) 18 { 19 this.hostName = hostName; 20 } 21 22 /// <summary> 23 /// Initializes a new instance of the <see cref="T:System.Web.Hosting.SimpleWorkerRequest"/> class for use in an arbitrary application domain, when the user code creates an <see cref="T:System.Web.HttpContext"/> (passing the SimpleWorkerRequest as an argument to the HttpContext constructor). 24 /// </summary> 25 /// <param name="appVirtualDir">The virtual path to the application directory; for example, "/app".</param> 26 /// <param name="appPhysicalDir">The physical path to the application directory; for example, "c:\app".</param> 27 /// <param name="page">The virtual path for the request (relative to the application directory).</param> 28 /// <param name="query">The text of the query string.</param> 29 /// <param name="output">A <see cref="T:System.IO.TextWriter"/> that captures the output from the response.</param> 30 /// <param name="hostName">The host name that will be requested.</param> 31 /// <exception cref="T:System.Web.HttpException">The <paramref name="appVirtualDir"/> parameter cannot be overridden in this context. 32 /// </exception> 33 public TestWorkerRequest(string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output, string hostName) 34 : base(appVirtualDir, appPhysicalDir, page, query, output) 35 { 36 this.hostName = hostName; 37 } 38 39 /// <summary> 40 /// Returns the server IP address of the interface on which the request was received. 41 /// </summary> 42 /// <returns> 43 /// The server IP address of the interface on which the request was received. 44 /// </returns> 45 public override string GetLocalAddress() 46 { 47 return hostName; 48 } 49 } |