如何防止通过http://mydomain.com/site/edit/1输入的方式加载部分视图?有没有什么方法可以做到这一点?
/ Martin
HTTP_X_REQUESTED_WITH
是否存在,其值等于XMLHttpRequest
。public class CheckAjaxRequestAttribute : ActionFilterAttribute
{
private const string AJAX_HEADER = "X-Requested-With";
public override void OnActionExecuting( ActionExecutingContext filterContext ) {
bool isAjaxRequest = filterContext.HttpContext.Request.Headers[AJAX_HEADER] != null;
if ( !isAjaxRequest ) {
filterContext.Result = new ViewResult { ViewName = "Unauthorized" };
}
}
}
[HttpGet]
[CheckAjaxRequest]
public virtual ActionResult ListCustomers() {
}
[ChildActionOnly]
属性就是你要找的。[ChildActionOnly]
public ActionResult Edit( int? id )
{
var item = _service.GetItem(id ?? 0);
return PartialView( new EditModel(item) )
}
Phil Haack有一篇文章在这里使用它