我有这个代码
@using (Ajax.BeginForm("LoadFilter", "Contact", new { filterType = (int)FilterContactBy.Name }, new AjaxOptions { OnBegin = "ShowProgrees", OnSuccess = "HideProgress" }))
{
@Html.TextBoxFor(m => m.ContactFilter.FilterValue, new { placeholder = "Enter a name" })
<input type="submit" class="link submit green" value="Add Filter" />
}
很明显,ShowProgress
和 HideProgress
方法只显示和隐藏加载器 gif。
后端的方法是
[HttpPost]
public ActionResult LoadFilter(ContactListModel filter, int filterType=0, Boolean isAllFilterApply=true)
{
if (filter != null)
{
// process here
}
}
例如,如果我输入任何内容都可以正常工作
hi
test
me & you
contact #1
contact #1 and me
但是
当我在任何错误中一起输入 时。加载器 gif 一直在移动, Controller 上的代码永远不会被命中。
此外,在 Chrome 控制台中,它显示 500 Internal Server Error
(Firefox 也是如此!)。
我的解决方案
我尝试了 escape
和 enocodeURIComponent
但没有成功。这是我试过的方法
$.ajaxPrefilter(function(o, org, xhr) {
if (org.url.indexOf('/Contact/LoadFilter?filterType=') > -1) {
// console.log(org.data[0].value);
org.data[0].value = encodeURIComponent(org.data[0].value);
// console.log(org.data[0].value);
}
});
输出(在控制台中)
&#
%26%23
当我使用 escape
而不是 encodeURIComponent
时,输出是相同的
&#
%26%23
但它仍然没有命中 Controller 上的方法。任何人都可以帮我找到解决方案,更重要的是,告诉我为什么首先会发生这种情况?
附上
请不要问我在联系人过滤器中输入 有什么用,或者删除这些并告诉客户这不是有效输入。我做不到,所以请不要建议。
更新
有人甚至完整地阅读了这篇文章吗?我不是在问我怎样才能解码。 否。请在将问题标记为重复或将其标记为关闭之前阅读完整问题。
最佳答案
好的,我明白了。
我在 Global.asax
中用这样的代码创建了 Application_BeginRequest
..
protected void Application_BeginRequest(Object sender, EventArgs e)
{
var httpApp = (HttpApplication)sender;
Debug.Write(httpApp);
}
我在 httpApp
下了一个断点,然后在 watch
窗口中检查了 httpApp.Request
并发现它在那里。它说 (我现在不记得了)抛出了类型为“System.Web.HttpRequestValidationException”的异常
。
所以,我知道错误是因为这个,所以在方法 LoadFilter
上,我只是添加了属性 ValidateInput(false)
,它正在工作
[HttpPost]
[ValidateInput(false)]
public ActionResult LoadFilter(ContactListModel filter, int filterType=0, Boolean isAllFilterApply=true)
{
// process here
}
不过,我确实必须在此处检查 sql 注入(inject),因为我们正在根据收到的输入构建动态查询。
关于ajax - 无法在 MVC 中传递与号和哈希符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24532337/