为什么不调用ExecuteResult?

时间:2014-06-30 18:17:08

标签: asp.net asp.net-mvc jsonp

我正在尝试在ASP.NET MVC 5 Web应用程序中实现JSONP响应。我已经按照本教程: http://www.ingeniumweb.com/blog/post/using-jsonp-calls-in-asp.net-mvc/1216/

问题是永远不会调用ExecuteResult方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace subscribe.ActionResults 
{
public class JsonpResult : JsonResult
{
    object Data = null;

    public JsonpResult()
    { 
    }

    public JsonpResult(object Data)
    {
        this.Data = Data;
    }

    public override void ExecuteResult(ControllerContext ControllerContext)
    {
        if (ControllerContext != null)
        {
            HttpResponseBase Response = ControllerContext.HttpContext.Response;
            HttpRequestBase Request = ControllerContext.HttpContext.Request;

            string callbackfunction = Request["callback"];
            if (string.IsNullOrEmpty(callbackfunction))
            {
                throw new Exception("Callback function name must be provided in the request!");
            }
            Response.ContentType = "application/x-javascript";
            if (Data != null)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Response.Write(string.Format("{0}({1});", callbackfunction, serializer.Serialize(Data)));
            }
        }
    }
}
}

在我的控制器中我有:

public JsonpResult Get(string emailaddress, string callback, string jsonp)
    {
        var slist = from u in db.subscriptions
                    select u;
        if (!String.IsNullOrEmpty(emailaddress))
            slist = slist.Where(s => (s.emailaddress == emailaddress));

        if (slist.Count() > 0)
        {
            JsonpResult result = new JsonpResult(slist.ToList());
            return result; //json list of matches
        }
        else
        {
            JsonpResult result = new JsonpResult(new List<Subscription>());
            return result; //empty json list
        }
    }

0 个答案:

没有答案