當URL請求(您的示例:string content
)包含危險關鍵字:「>,?,<等......」。 如果你想處理這個異常(例如:允許這些),可以按照示例代碼:
using System;
using System.Web.Mvc;
namespace Custom {
public class CustomValidateInput : FilterAttribute, IAuthorizationFilter {
/// <summary>
/// Called when authorization is required.
/// </summary>
/// <param name="filterContext">The filter context.</param>
/// <exception cref="System.ArgumentNullException">filterContext</exception>
public void OnAuthorization(AuthorizationContext filterContext) {
if (filterContext == null) {
throw new ArgumentNullException("filterContext");
}
//Set disable validation request
filterContext.Controller.ValidateRequest = false;
//----------Your code handle here------------//
}
}
}
和
[CustomValidateInput]
public ActionResult Save(string content)
{
System.IO.File.WriteAllText(fileName, content);
return View();
}
告訴我怎麼讀你的屬性的「內容」參數? – user2217261
之前,您添加對「System.Web.Routing」的新引用並添加新代碼:if(filterContext.RequestContext.HttpContext.Request.Url!= null)var content = filterContext.RequestContext.HttpContext.Request.Url .Query; }' – LazyCatIT
因爲你不直接使用'filterContext.RequestContext.HttpContext.Request [「content」]''。 – LazyCatIT