gpt4 book ai didi

c# - 数据验证 ASP MVC OnActionExecuting

转载 作者:行者123 更新时间:2023-11-30 22:25:08 27 4
gpt4 key购买 nike

我创建了一个基本 Controller ,我的每个 Controller 都继承了它。在这个 Controller 中,我有 OnActionExecuting 方法。我用它来检查一些参数的 url。我遇到的问题是每当我发布 html 数据时都会出现异常。我已经使用 [AllowHTML] 标签设置了模型,它适用于所有其他操作。

如何让 OnActionExecuting 方法关注模型验证?

这是我的基本 Controller 中的内容

public abstract class BaseController : Controller
{
[ValidateInput(false)]
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if ((Request.Params["api"] == null || string.IsNullOrEmpty(Request.Params["api"])))
return;

if ((Request.Params["api"] != null && !string.IsNullOrEmpty(Request.Params["api"])))
{
if (Session["api"] == null)
{
Session["api"] = Request.Params["api"];
}
}
}

下面是我模型的摘录

[MetadataType(typeof (MessagingMetaData))]
public partial class Message
{

}

public class MessagingMetaData
{
[Required]
[Display(Name = "Message")]
[DataType(DataType.Html)]
[AllowHtml]
public string Body { get; set; }
}

这是堆栈跟踪

[System.Web.HttpRequestValidationException]

Exception Message: A potentially dangerous Request.Form value was detected from the client (Content="

sdafdsafdsafdsac__DisplayClass12.b__d(String value, String key) at Microsoft.Web.Infrastructure.DynamicValidationHelper.LazilyEvaluatedNameObjectEntry.ValidateObject() at Microsoft.Web.Infrastructure.DynamicValidationHelper.LazilyEvaluatedNameObjectEntry.GetValidatedObject() at Microsoft.Web.Infrastructure.DynamicValidationHelper.LazilyValidatingArrayList.get_Item(Int32 index) at System.Collections.Specialized.NameObjectCollectionBase.BaseGetKey(Int32 index) at System.Collections.Specialized.NameValueCollection.GetKey(Int32 index) at System.Collections.Specialized.NameValueCollection.Add(NameValueCollection c) at System.Web.HttpRequest.FillInParamsCollection() at System.Web.HttpRequest.GetParams() at System.Web.HttpRequest.get_Params() at System.Web.HttpRequestWrapper.get_Params() at ProjectX.BaseController.OnActionExecuting(ActionExecutingContext filterContext) at System.Web.Mvc.Controller.System.Web.Mvc.IActionFilter.OnActionExecuting(ActionExecutingContext filterContext) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.b__14() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) at System.Web.Mvc.Controller.ExecuteCore() at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.b__5() at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.b__0() at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.b__7(IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) at System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.b__d() at System.Web.Mvc.SecurityUtil.b__0(Action f) at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

抱歉布局由于某种原因不会复制新行

最佳答案

我相信您应该能够使用Unvalidated() 扩展方法。例如,将 Request.Params["api"] 更改为 Request.Unvalidated().Params["api"]

关于c# - 数据验证 ASP MVC OnActionExecuting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12464856/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com