我正在尝试创建一个类似于Web服务器的小程序,可以接受URL参数。我找到了这个示例项目:https://codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx。有没有办法让它接收我的URL参数?还有其他具有此功能的示例项目吗?
public class Startup1
{
public void Configuration(IAppBuilder app)
{
app.Run(context =>
{
var value = context.Request.Query.Get("someKey");
if (value == "foo")
{
// do something
}
context.Response.ContentType = "text/plain";
return context.Response.WriteAsync("Hello, world.");
});
}
}
http://someServer:80/?someKey=foo
,其中包含IT技术相关内容。请注意,保留了HTML标签。class Program
{
static void Main(string[] args)
{
var url = "http://localhost:8080";
WebApp.Start(url, builder => builder.UseFileServer(enableDirectoryBrowsing:true));
Console.WriteLine("Listening at " + url);
Console.ReadLine();
}
}
根据您的项目示例,尝试将以下内容放入SendRequest方法中:
string name = request.QueryString.Get("name");
switch (name){
case "a":
return string.Format("<HTML><BODY>My web page a.<br>{0}</BODY></HTML>", DateTime.Now);
break;
case "b":
return string.Format("<HTML><BODY>My web page b.<br>{0}</BODY></HTML>", DateTime.Now);
break;
default:
return string.Format("<HTML><BODY>My web page .<br>{0}</BODY></HTML>", DateTime.Now);
break;
}