일반 조치 필터가 있으며 OnActionExecuting
방법으로 현재 모델을 가져오고 싶습니다. 현재 구현은 다음과 같습니다.조치 필터에서 현재 모델을 얻는 방법
public class CommandFilter<T> : IActionFilter where T : class, new()
{
public void OnActionExecuting(ActionExecutingContext actionContext)
{
var model= (T)actionContext.ActionArguments["model"];
}
}
내 모든 모델 이름이 동일하면 잘 작동합니다. 하지만 differnet 모델 이름을 사용하고 싶습니다.
이 문제를 해결하는 방법은 무엇입니까?
당신은 모델에 접근 할 수 base MVC Controller에 ActionExecutingContext.Controller
재산
/// <summary>
/// Gets the controller instance containing the action.
/// </summary>
public virtual object Controller { get; }
및 변환 결과를 사용할 수 있습니다
public class HomeController : Controller
{
[ServiceFilter(typeof(CommandActionFilter<CreateInput>))]
public IActionResult Create([FromBody]CreateInput model)
{
return new OkResult();
}
}
같은 http://stackoverflow.com/questions/872796/asp-net-mvc-accessing-view-model-from-a-custom을 쓸 수 있습니다 경우 -action-filter – user2184057
@ user2184057 이것은보기 모델 용이며 요청 모델 – Set