在asp.net页面中添加querystring后获得404
本文关键字:querystring 添加 asp net | 更新日期: 2023-09-27 18:11:17
我有一个简单的页面,应该显示一个图像,这取决于在查询字符串中发送它的路径。我得到一个404错误代码后,我添加查询字符串。
www.domain.com/ViewImage.aspx ?图像=/img/image.jpg
ViewImage.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="baseline">
<img alt="image" id="Img2" runat="server" class="fullimage" src="" />
</td>
</tr>
</table>
</form>
</body>
</html>
ViewImage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
var src = Server.UrlDecode(this.Request.QueryString["Image"]);
this.Img2.Src = validInput(src);
}
protected string validInput(string input)
{
var regex = "[''<>'"]";
if (null != input && !input.Contains("'"") && input.StartsWith("/"))
{
return !Regex.IsMatch(input, regex) ? AntiXssEncoder.XmlAttributeEncode(input):string.Empty;
}
return string.Empty;
}
预期结果
<img alt="image" id="Img2" runat="server" class="fullimage" src="/img/image.jpg" />
当前结果错误404
这是我尝试和检查的:
验证页面存在
www.domain.com/ViewImage.aspx工作正常,没有src图像集,但正在正确地找到页面。
验证图像存在
www.domain.com/img/image.jpg工作正常,img显示正确
尝试与错误的路径
www.domain.com/ViewImage.aspx ? =/asdasdas/asdas.jpg形象我没有得到404错误和图像src设置正确
尝试在url中只有文件夹而没有图像
www.domain.com/ViewImage.aspx?Image=/img,这不会得到404错误,但如果添加最后一个斜杠,我也会得到404错误。
www.domain.com/ViewImage.aspx ?图像=/img/
最后一个url出现404错误。
注释:
- 我们在服务器上使用SSL (https),不确定这是否重要
- 我认为可能错误可以/应该在IIS配置中修复,不确定什么或如何修复。
有什么建议吗?
要记住的一件事是不支持查询字符串值中的/
,尽管有些浏览器可能能够在您在URL中输入它时为您找出编码它。路径应该是uri编码的(如果是/
,则是%2F
)。这是可能的,你的服务器配置是这样的/
被用于某种路径解析,导致404;如果您的查询字符串没有格式化,则可能没有此问题。
你也可以简化你的代码。Request.QueryString
自动解码参数,因此您不必手动执行:
protected void Page_Load(object sender, EventArgs e)
{
// The query string is automatically decoded
var src = this.Request.QueryString["Image"];
this.Img2.Src = validInput(src);
}
protected string validInput(string input)
{
var regex = "[''<>'"]";
if (null != input && !input.Contains("'"") && input.StartsWith("/"))
{
return !Regex.IsMatch(input, regex) ? AntiXssEncoder.XmlAttributeEncode(input):string.Empty;
}
return string.Empty;
}