1

为了解决一个荒谬的问题,我需要在我的查询字符串中附加一个值(在 javascript 中这样做)并测试它是否存在于服务器上(因为这可能来自 ajax 或 iframe,所以遗憾的是没有标题潜力,并且我试图避免向我的<form>元素添加值)。为此,我设计了这个小片段,但我不确定将“setter”块放在哪里:

using System.Web.Mvc;

namespace Company.Client.Framework.Mvc
{
  class CreateFormDialogResponseAttribute : ActionFilterAttribute
  {
    private bool SetContentType { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        SetContentType = !filterContext.HttpContext.Request.Params["FormDialogRequest"].IsEmpty();

        base.OnActionExecuting(filterContext);
    }
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    { 
        //do I set it here? (A)
        if (SetContentType)
            filterContext.HttpContext.Response.ContentType = "text/json";
        base.OnResultExecuting(filterContext);
    }
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        //do I set it here? (B)
        if (SetContentType)
            filterContext.HttpContext.Response.ContentType = "text/json";
        base.OnResultExecuted(filterContext);
    }
  }
}

如果我将其设置为,(A)那么客户端似乎仍有时间“覆盖”该属性(如果需要)。而(B)看起来像“我不在乎你认为你想做什么”的立场。这是一个准确的理解吗?

4

1 回答 1

0

self-answer:

Because I need to override the value set by the ActionResult itself, I have to come in after the method as already done it's work. Because I want to avoid the situation where the developer has manually set the type, I'm doing two checks, one to see if we should set it, and one to see if it's "application/json" (the default).

于 2013-01-02T23:45:06.133 回答