开发者

ActionResult helper and different types resolver

Idea is to use sa开发者_如何学Cme action methods for different types of results I.E.

/category/details/?resultFormat=json

/category/details/?resultFormat=xml

So to have some kind of ActionResult helper that contains registered pairs of value resolvers

"json", JsonValueResolver

"xml", XmlResolver

etc...is there already solution for this or I have to think some kind of custom resolver? Automapper has good solution for value resolving. Any ideas?


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; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜