【问题标题】:IHttpModule filter write method is not called asp.netIHttpModule 过滤器写入方法不调用 asp.net
【发布时间】:2012-06-03 11:14:55
【问题描述】:

我正在尝试更改响应以获取 jsonp(用于 Web 服务),但问题是永远不会调用 write 方法,最后只调用了 flush(),url 是 本地主机:8080/api/employees

它的调用web api和员工是一个控制器文件,

被调用的控制器方法

public List<Models.Employee> Get() {

            var employees = from e in _context.Employees
                            select new Models.Employee {
                                Id = e.EmployeeID,
                                FirstName = e.FirstName,
                                LastName = e.LastName
                            };


            return employees.ToList();

}

被调用的IHTTPModule方法

public void OnReleaseRequestState(object sender, EventArgs e)
        {
            HttpResponse response = HttpContext.Current.Response;
            if (response.ContentType != JSON_CONTENT_TYPE && response.ContentType != JSON_CONTENT_TYPE1) return;

            //response.ContentEncoding =
           // char[] c = { 'd', 's', 'w', 'w' };

            response.Filter = new JsonResponseFilter(response.Filter);
            //response.Cache.
           // response.ClearContent();
           // response.Write(c, 0, 4);
           // response.Flush();
        }

现在不调用 JsonResponseFilter.write 就是这个问题,请任何人回复以解决它

【问题讨论】:

    标签: asp.net jsonp asp.net-web-api


    【解决方案1】:

    你做错了。不要使用任何 IHttpModules。在 Web API 中实现此目的的正确方法是使用自定义 media formatter。例如,您可以将 JsonpFormatter 集成到 Application_Start 中,这将为您的 Web 方法启用 JSONP:

    var config = GlobalConfiguration.Configuration;
    config.Formatters.Insert(0, new JsonpMediaTypeFormatter());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      相关资源
      最近更新 更多