如何使用WebClient.DownloadData(到本地DummyPage.aspx)
我正在关注此链接上的教程 http://www.codeproject.com/KB/ aspnet/ASPNETService.aspx
现在我被这些代码困住了
private const string DummyPageUrl =
"http://localhost/TestCacheTimeout/WebForm1.aspx";
private void HitPage()
{
WebClient client = new WebClient();
client.DownloadData(DummyPageUrl);
}
我的本地应用程序地址在“localhost”后面有一个端口号,那么我怎样才能获得完整路径(可以在Application_Start方法中完成)?我希望它非常通用,以便它可以在任何情况下工作。
多谢!
更新
我在 Application_Start 中尝试了这个,它运行良好,但在发布到 IIS7 时立即返回错误
String path = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/");
I'm following a tutorial on this link http://www.codeproject.com/KB/aspnet/ASPNETService.aspx
Now I'm stuck at these codes
private const string DummyPageUrl =
"http://localhost/TestCacheTimeout/WebForm1.aspx";
private void HitPage()
{
WebClient client = new WebClient();
client.DownloadData(DummyPageUrl);
}
My local application address has a port number after "localhost", so how can I get the full path (can it be done in Application_Start method)? I want it to be very generic so that it can work in any cases.
Thanks a lot!
UPDATE
I tried this in the Application_Start and it runs fine, but return error right away when published to IIS7
String path = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果它回调到同一服务器,也许可以使用
Request
对象:否则,将其他服务器的详细信息存储在配置文件或数据库中,然后为其提供正确的值。
但更好的问题是:为什么你要通过 http 与自己交谈??为什么不直接调用类方法呢?就我个人而言,我会使用外部计划作业来执行此操作。
If it is calling back to the same server, perhaps use the
Request
object:Otherwise, store the other server's details in a config file or the database, and just give it the right value.
But a better question would be: why would you talk via http to yourself? Why not just call a class method? Personally I'd be using an external scheduled job to do this.
当您部署到可能具有虚拟应用程序文件夹的不同环境时,您需要一个有效的答案。
这适用于所有情况,并且在部署时不会出现令人讨厌的意外。
You need an answer that works when you roll out to a different environment that maybe has a virtual application folder.
This will work in all cases and no nasty surprises when you deploy.
我怀疑您使用的是 Visual Studio 内置的 ASP.NET 开发服务器,默认情况下它倾向于更改端口号。如果是这种情况,那么您可以尝试简单地将开发服务器配置为始终使用相同的端口,如此处所述。然后只需将端口号添加到您的 URL,如下所示:
I suspect you're using the ASP.NET development server that's built-in to Visual Studio, which has a tendency to change port numbers by default. If that's the case, then you might try simply configuring the development server to always use the same port, as described here. Then just add the port number to your URL, like so: