asp.net在打开弹出窗口之前检查URL是否存在 [英] asp.net check if url exists before opening popup
问题描述
当前,我在ButtonClick上运行以下代码:
Currently I have the following code that is run on ButtonClick:
Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow",
"window.open('" + DocumentData.Tables[0].Rows[0]["WebAddress"].ToString()
+ "','_blank');", true);
它会打开一个弹出窗口,其中包含来自数据库的给定链接(通常是指向文档,图像或视频的链接).但是我需要稍微修改一下代码,而且我不知道应该使用哪种方法:
It opens popup window with given link from DataBase (usually it is a link to document or image or video). But I need to modify code a bit and I do not know which methods should I use:
1)我需要检查url是否确实存在(如果URL响应),如果不存在,请不要打开弹出窗口,而是显示一些消息.在这里我不知道如何检查网址是否存在?例如,如果url类似于www.thesitedoesntexists.com,则不要加载弹出窗口.
1) I need to check if url really exists (if URL is responsive) and if not do not open popup, but show some message. Here I do not know how to check if Url exists? For example, if url is something like www.thesitedoesntexists.com then Do not load popup.
2)如果url的格式为www.yahoo.com而不是 http://www.yahoo.com 或 https://someurl.com ,则以上操作无效.
2) If url is in format www.yahoo.com instead of http://www.yahoo.com or https://someurl.com then above doesn't work.
如果我的Web应用程序是 www.myapplication.com
,则在上述情况下,系统将打开URL www.myapplication.com/www.yahoo.com
而不是www.yahoo.com
.怎么处理呢?这可能与问题#1相关.这是主要问题.
If my web application is www.myapplication.com
then in above scenario system opens url www.myapplication.com/www.yahoo.com
instead of www.yahoo.com
. How to deal with it? It could be somehow related with question #1. And this is the main problem.
推荐答案
以下代码(未经测试)应完成以下任务:
The following code (untested) should accomplish these tasks:
- 将验证网址是否已定义
- 将确保使用http://或https://
- 将通过HttpRequest验证URL是否存在
- 将显示加载了URL的弹出窗口,或者如果URL未定义或不存在,则显示警告警报.
按钮点击处理程序代码:
Button click handler code:
string url = DocumentData.Tables[0].Rows[0]["WebAddress"].ToString();
string script;
if (!string.IsNullOrEmpty(url))
{
// prepend http to url if it isn't there.
if(!url.ToLower().StartsWith("http://") || !url.ToLower().StartsWith("https://"))
{
url = "http://" + url;
}
// verify URL exists:
if (UrlExists(url))
{
script = "window.open('" + url + "','_blank');";
}
else
{
script = "alert('URL does not exist')";
}
}
else
{
script = "alert('No URL specified!')";
}
Page.ClientScript.RegisterStartupScript(this.GetType(), "WindowScript", script, true);
并在您的课程中定义以下URL检查方法:
and define the following URL check method in your class:
public static bool UrlExists(string url)
{
try
{
var request = WebRequest.Create(url) as HttpWebRequest;
if (request == null) return false;
request.Method = "HEAD";
using (var response = (HttpWebResponse)request.GetResponse())
{
return response.StatusCode == HttpStatusCode.OK;
}
}
catch (UriFormatException)
{
//Invalid Url
return false;
}
catch (WebException)
{
//Unable to access url
return false;
}
}
我完全相信UrlExists方法能够: http://paigecsharp.blogspot.ca/2011/01/verify-url-exists.html
I give full credit to the UrlExists method to: http://paigecsharp.blogspot.ca/2011/01/verify-url-exists.html
这篇关于asp.net在打开弹出窗口之前检查URL是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!