コントローラでアクションをマークして、ajax呼び出しとRenderActionの両方から呼び出すことができるようにします。問題は、この両方の属性が異なる抽象を派生または実装していることです。 1つの方法は、次のとおりです:AjaxOnlyAttributeとChildActionOnlyAttributeを1つのアクションフィルタに結合する
[AjaxOnly]
PartialViewResult GetViewAjax(int foo) { return GetView(foo); }
[ChildActionOnly]
PartialViewResult GetView(int foo) { ... }
これはまったくまったくありません。
AjaxOnlyは私が話しています属性です:
public sealed class AjaxOnlyAttribute : ActionFilterAttribute
{
#region Public members
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext == null)
throw new ArgumentNullException("filterContext");
if (filterContext.HttpContext.Request.Headers["X-Requested-With"] != "XMLHttpRequest")
filterContext.Result = new HttpNotFoundResult();
}
#endregion
}
この方法は、MVC3先物から取得されます。条件は、以下の開発チームによって行われたと述べていたではないfilterContext.HttpContext.Request.IsAjaxRequest()
されている理由の重要なコメントは:
// Dev10 #939671 - If this attribute is going to say AJAX *only*, then we need to check the header
// specifically, as otherwise clients can modify the form or query string to contain the name/value
// pair we're looking for.
なぜですか?あなたのアクションが、RenderAction()とajaxリクエストの両方を介して異なる場所から再利用されたとします。これらが異なるページで再利用され、多くの場所でいくつかの標準ツールパネルを描画し、さまざまなコンテキストでわずかに異なるものが使用されていると仮定します。 –
@Hohhiしたがって、AJAX呼び出しまたはChildアクション? –
まさに、あなたの提案をありがとう、私は質問を更新し、いくつかの時間を取得する方法を教えてください –