0

想法是对不同类型的结果使用相同的操作方法 IE

/category/details/?resultFormat=json

/category/details/?resultFormat=xml

所以要有某种包含已注册值解析器对的 ActionResult 帮助器

"json", JsonValueResolver

"xml", XmlResolver

等等...是否已经有解决方案或者我必须考虑某种自定义解析器?Automapper 对值解析有很好的解决方案。有任何想法吗?

4

1 回答 1

1
public class SmartResult : ActionResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }

        if (context.HttpContext.Request.QueryString["ResultFormat] == "json")
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            context.HttpContext.Response.Write(serializer.Serialize(this.Data));
        } else if(context.HttpContext.Request.QueryString["ResultFormat] == "xml")
                    {
              ...serialize using xmlserializer
        }else{
            throw new InvalidOperationException();
        }
    }

    public object Data { get; set; }
}
于 2009-11-18T13:58:08.557 回答