public void ProcessRequest(MyHttpContext context) { //获得请求信息 context.Request //根据请求信息获取响应内容 string baseWebPath = MyConfigUtil.Get("baseWebPath"); string fileName = Path.Combine(baseWebPath, context.Request.Url.TrimStart('/')); //根据请求的URL判断请求文件的类型 string contentType = MyMIMEDict.GetType(Path.GetExtension(context.Request.Url)); string dfileName = fileName; #region 处理动态文件 string ext = Path.GetExtension(context.Request.Url); if (ext == ".aspx") { var className = Path.GetFileNameWithoutExtension(context.Request.Url); //应为所有的aspx的类都继承了IHttpHandler 所以这里可以将实例化的类型设置为接口类型来接收, 以便使用接口定义的方法 IMyHttpHandler obj = Assembly.Load("MyWebServer").CreateInstance("MyWebServer.web." + className, true) as IMyHttpHandler; if (obj != null) { obj.ProcessRequest(context); return;//终止执行 } } #endregion #region 处理静态文件 if (Directory.Exists(fileName) && (File.Exists(Path.Combine(fileName, "index.html")))) { dfileName = Path.Combine(fileName, "index.html".TrimStart('/')); contentType = "text/html"; } else if (Directory.Exists(fileName) && File.Exists(Path.Combine(fileName, "default.html"))) { dfileName = Path.Combine(fileName, "default.html".TrimStart('/')); contentType = "text/html"; } if (!File.Exists(dfileName)) { NotExistResp(context); } else { context.Response.StateCode = "200"; context.Response.StateDes = "OK"; //获取请求文件的类型 // Path.GetExtension(context.Request.Url); context.Response.ContentType = contentType; context.Response.Body = File.ReadAllBytes(dfileName); } #endregion }
private void MainServer_Load(object sender, EventArgs e) { //加载配置信息 if (MyConfigUtil.KeyExist("baseWebPath")) { //读取配置文件数据 string baseWebPath = MyConfigUtil.Get("baseWebPath"); textBox_basePath.Text = baseWebPath; } label_online.Text = ""; }
private void btn_selectPath_Click(object sender, EventArgs e) { FolderBrowserDialog path = new FolderBrowserDialog(); DialogResult ret = path.ShowDialog(); if (ret == DialogResult.OK) { textBox_basePath.Text = path.SelectedPath; //保存配置文件 MyConfigUtil.Save("baseWebPath", path.SelectedPath); } }