惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
G
Google Developers Blog
B
Blog
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 叶小钗
The Cloudflare Blog
The Hacker News
The Hacker News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
雷峰网
雷峰网
F
Fortinet All Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
量子位
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
H
Help Net Security
Help Net Security
Help Net Security
P
Palo Alto Networks Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
T
The Exploit Database - CXSecurity.com
Know Your Adversary
Know Your Adversary
Apple Machine Learning Research
Apple Machine Learning Research
Scott Helme
Scott Helme
N
News | PayPal Newsroom
AWS News Blog
AWS News Blog
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
腾讯CDC
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
TaoSecurity Blog
TaoSecurity Blog
GbyAI
GbyAI
Y
Y Combinator Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Docker

博客园 - xiaosonl

学习手札#3 NHibernate缓存 产品的简单性 关于过度设计的思考(上) 学习手札#2 故事点和小时数的思考 学习笔记#1 键值对数据库 SQLite数据迁移 探讨一种在Silverlight不普及情况下的部署策略 有用的文档 Silverlight产品布署策略 探讨一种Silverlight的异步编程模式 代码的注释 下半年要看完消化的技术类书籍 中小型企业的人员流失 谈谈Ruby On Rails和ASP.NET 工作中的系统学习 Uml中的关联与依赖关系 TDD与重构设计 C#中使用位运算来实现权限管理 Silverlight中JavaSciprt无法访问托管类抽象成员的解决方法
让ASP.NET MVC的Controller输出不同类型数据
xiaosonl · 2010-05-30 · via 博客园 - xiaosonl

2010-05-30 13:58  xiaosonl  阅读(1223)  评论()    收藏  举报

ASP.NET MVC中,可以通过返回不同类型的ActionResult来输出不同内容,比如ViewResult会输出视图页,JsonResult会输出Json数据等等。

而有时会遇到同一个Controller需要支持输出不同类型的情况,比如正常查看一个用户的资料页时, 用/User/{id}就可以访问到;而在JavaScript或其它系统中需要查看用户资料,又希望/User/{id}能返回Json数据。 这种情况可以通过构建一个自定义ActionResult来实现,根据参数返回默认的ViewResult,或者将Model序列化为指定的类型输出,其实就是控制Model的输出行为。输出类型的参数,可以通过两种方式来传递,一个种传统的Get或Post传参:/User/{id}?rtype=json;另一种可以通过Http请求的ContentType参数来指定类型:request.ContentType = “json”.

附参考代码:

    public class AutoResult : ActionResult
    {
        public string ViewName { get; set; }
        public object Model { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            string type = context.HttpContext.Request["rtype"];
            if (string.IsNullOrEmpty(type))
            {
                type = context.HttpContext.Request.ContentType;
            }

            if (!string.IsNullOrEmpty(type))
            {
                switch (type.ToLower())
                {
                    case "json":
                        new NJsonResult(Model).ExecuteResult(context);
                        break;
                    case "binary":
                        new BinaryResult(Model).ExecuteResult(context);
                        break;
                    case "xml":
                        new XmlResult(Model).ExecuteResult(context);
                        break;
                    default:
                        context.HttpContext.Response.Output.Write("无法识别的类型");
                        break;
                }
            }
            else
            {
                context.Controller.ViewData.Model = Model;
                ViewResult viewResult = new ViewResult() { ViewName = ViewName, ViewData = context.Controller.ViewData };
                viewResult.ExecuteResult(context);
            }
        }
    }