Is there any attribute relating to AJAX to be set for ASP.NET MVC controller actions?

后端 未结 6 897
轻奢々
轻奢々 2020-11-30 02:13

I want to use partial views with AJAX calls in ASP.NET MVC, and this is the first time I\'m using it. I just searched to see if there is anything special I should know befor

6条回答
  •  执念已碎
    2020-11-30 02:55

    my solution follows the [ChildActionOnly] implementation:

    public class AjaxOnlyAttribute : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext == null)
                throw new ArgumentNullException("filterContext");
            if (!filterContext.HttpContext.Request.IsAjaxRequest())
                throw new InvalidOperationException(string.Format(
                    CultureInfo.CurrentCulture, 
                    "The action '{0}' is accessible only by an ajax request.", 
                    filterContext.ActionDescriptor.ActionName
                ));
        }
    }
    

提交回复
热议问题