让ASP.NET接受有“潜在危险”的提交
什么是有“潜在危险”的提交?马上动手写个简单的例子:
用Visual Studio创建一个空白的ASP.NET MVC程序,一切默认即可,添加一个空白的HomeController,增加一个Index Action及一个Index View,View也可以暂时留空白,运行这个程序,一切都没毛病,好,现在在地址栏上敲入: http://localhost:37538/%3Cscript%3E
出现:
“从客户端中检测到有潜在危险的Request.Path值”,当然了,你还可以在地址栏上尝试点别的,比如一些特殊符号,也可能出现这种情况,注意一下,现在出现的异常为:HttpException。
现在我们尝试别的情况,改一下Controller:
public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(string p1) { ViewBag.P1 = p1; return View(); } }
再改一下View:
DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Indextitle>
head>
<body>
<div>
<p>@ViewBag.P1p>
<form method="post">
<input type="text" name="p1" />
<input type="submit" />
form>
div>
body>
html>
代码很简单,我们尝试给p1写点值,然后回显,输入个什么“123”或者“abc”是没问题的,但如果尝试输入“”或者“