动作过滤器永远不会在ASP.NET MVC 3中被击中

时间:2011-11-12 17:58:56

标签: asp.net-mvc-3 jsonp action-filter actionfilterattribute

我不知道为什么我的行动没有被击中。该控制器位于名为api的“区域”下。

$.ajax({
            url: defaults.url + (defaults.url.indexOf('?') > 0 ? '&' : '?') + 'r=' + Math.random(),
            type: defaults.method,
            contentType: 'application/json',
            dataType: 'json',
            data: defaults.data,

            success: function (data) {


    public class EventController : Controller
        {
            [JsonpFilter(Order = 1)]
            public JsonResult Register()
            {

                return new JsonResult
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = new ApiRegistrationResponse()
                };
            }
        }

[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
    public class JsonpFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext == null)
                throw new ArgumentNullException("filterContext");

            string callback = filterContext.HttpContext.Request.QueryString["callback"];

            if (!string.IsNullOrEmpty(callback))
            {
                var result = filterContext.Result as JsonResult;
                if (result == null)
                {
                    throw new InvalidOperationException("JsonpFilterAttribute must be applied only " +
                        "on controllers and actions that return a JsonResult object.");
                }

                filterContext.Result = new JsonpResult
                {
                    ContentEncoding = result.ContentEncoding,
                    ContentType = result.ContentType,
                    Data = result.Data,
                    Callback = callback
                };
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

经过反复试验,我不得不在NinjectMVC3.cs文件夹下排除以下ninject文件App_Start,它就开始工作了。

[assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication2.App_Start.NinjectMVC3), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MvcApplication2.App_Start.NinjectMVC3), "Stop")]

namespace MvcApplication2.App_Start
{
    using System.Reflection;
    using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    using Ninject;
    using Ninject.Web.Mvc;

    public static class NinjectMVC3 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
            DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            RegisterServices(kernel);
            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
        }        
    }
}