关于Action返回结果类型的事儿(下)

简介: 原文:关于Action返回结果类型的事儿(下) using System;  using System.Collections.Generic;  using System.Linq;  using System.
原文: 关于Action返回结果类型的事儿(下)

using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using System.Web.Mvc.Ajax;
   
  using System.IO;
   
  namespace MVC.Controllers
  {
      /// <summary>
      /// Controller 类必须以字符串 "Controller" 做类名称的结尾,字符串 Controller 之前的字符串为 Controller 的名称,类中的方法名为 Action 的名称
      /// </summary>
      public class ControllerDemoController : Controller
      {
          // [NonAction] - 当前方法仅为普通方法,不解析为 Action
          // [AcceptVerbs(HttpVerbs.Post)] - 声明 Action 所对应的 http 方法
   
          /// <summary>
          /// Action 可以没有返回值
          /// </summary>
          public void Void()
          {
              Response.Write( string .Format( "<span style='color: red'>{0}</span>" , "void" ));
          }
   
          /// <summary>
          /// 如果 Action 要有返回值的话,其类型必须是 ActionResult
          /// EmptyResult - 空结果
          /// </summary>
          public ActionResult EmptyResult()
          {
              Response.Write( string .Format( "<span style='color: red'>{0}</span>" , "EmptyResult" ));
              return new EmptyResult();
          }
   
          /// <summary>
          /// Controller.Redirect() - 转向一个指定的 url 地址
          /// 返回类型为 RedirectResult
          /// </summary>
          public ActionResult RedirectResult()
          {
              return base .Redirect( "~/ControllerDemo/ContentResult" );
          }
   
          /// <summary>
          /// Controller.RedirectToAction() - 转向到指定的 Action
          /// 返回类型为 RedirectToRouteResult
          /// </summary>
          public ActionResult RedirectToRouteResult()
          {
              return base .RedirectToAction( "ContentResult" );
          }
   
          /// <summary>
          /// Controller.Json() - 将指定的对象以 JSON 格式输出出来
          /// 返回类型为 JsonResult
          /// </summary>
          public ActionResult JsonResult( string name)
          {
              System.Threading.Thread.Sleep(1000);
  
              var jsonObj = new { Name = name, Age = new Random().Next(20, 31) };
              return base .Json(jsonObj);
          }
   
          /// <summary>
          /// Controller.JavaScript() - 输出一段指定的 JavaScript 脚本
          /// 返回类型为 JavaScriptResult
          /// </summary>
          public ActionResult JavaScriptResult()
          {
              return base .JavaScript( "alert('JavaScriptResult')" );
          }
   
          /// <summary>
          /// Controller.Content() - 输出一段指定的内容
          /// 返回类型为 ContentResult
          /// </summary>
          public ActionResult ContentResult()
          {
              string contentString = string .Format( "<span style='color: red'>{0}</span>" , "ContentResult" );
              return base .Content(contentString);
          }
   
          /// <summary>
          /// Controller.File() - 输出一个文件(字节数组)
          /// 返回类型为 FileContentResult
          /// </summary>
          public ActionResult FileContentResult()
          {
              FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif" , FileMode.Open);
              int length = ( int )fs.Length;
              byte [] buffer = new byte [length];
              fs.Read(buffer, 0, length);
              fs.Close();
   
              return base .File(buffer, "image/gif" );
          }
   
          // <summary>
         /// Controller.File() - 输出一个文件(文件地址)
          /// 返回类型为 FileContentResult
          /// </summary>
          public ActionResult FilePathResult()
          {
              var path = Request.PhysicalApplicationPath + "Content/loading.gif" ;
              return base .File(path, "image/gif" );
          }
  
          // <summary>
         /// Controller.File() - 输出一个文件(文件流)
          /// 返回类型为 FileContentResult
          /// </summary>
          public ActionResult FileStreamResult()
          {
              FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif" , FileMode.Open);
   
              return base .File(fs, @"image/gif" );
          }
  
          /// <summary>
         /// HttpUnauthorizedResult - 响应给客户端错误代码 401(未经授权浏览状态),如果程序启用了 Forms 验证,并且客户端没有任何身份票据,则会跳转到指定的登录页
          /// </summary>
          public ActionResult HttpUnauthorizedResult()
          {
              return new HttpUnauthorizedResult();
          }
   
/// <summary>
/// Controller.PartialView() - 寻找 View ,即 .ascx 文件
/// 返回类型为 PartialViewResult
/// </summary>
     public ActionResult PartialViewResult()
     {
         return base .PartialView();
      }
  
         /// <summary>
          /// Controller.View() - 寻找 View ,即 .aspx 文件
          /// 返回类型为 ViewResult
          /// </summary>
          public ActionResult ViewResult()
          {
              // 如果没有指定 View 名称,则寻找与 Action 名称相同的 View
              return base .View();
          }
   
          /// <summary>
          /// 用于演示处理 JSON 的
          /// </summary>
          public ActionResult JsonDemo()
          {
             return View();
          }
   
        /// <summary>
          /// 用于演示上传文件的
        /// </summary>
          public ActionResult UploadDemo()
         {
             return View();
          }
   
          /// <summary>
          /// 用于演示 Get 方式调用 Action
          /// id 是根据路由过来的;param1和param2是根据参数过来的
          /// </summary>
          [AcceptVerbs(HttpVerbs.Get)]
          public ActionResult GetDemo( int id, string param1, string param2)
          {
              ViewData[ "ID" ] = id;
              ViewData[ "Param1" ] = param1;
              ViewData[ "Param2" ] = param2;
   
              return View();
         }
   
          /// <summary>
          /// 用于演示 Post 方式调用 Action
          /// </summary>
          /// <remarks>
          /// 可以为参数添加声明,如:[Bind(Include = "xxx")] - 只绑定指定的属性(参数),多个用逗号隔开
          /// [Bind(Exclude = "xxx")] - 不绑定指定的属性(参数),多个用逗号隔开
          /// [Bind] 声明同样可以作用于 class 上
          /// </remarks>
          [AcceptVerbs(HttpVerbs.Post)]
          public ActionResult PostDemo(FormCollection fc)
          {
              ViewData[ "Param1" ] = fc[ "param1" ];
              ViewData[ "Param2" ] = fc[ "param2" ];
   
              // 也可以用 Request.Form 方式获取 post 过来的参数
   
              // Request.Form 内的参数也会映射到同名参数。例如,也可用如下方式获取参数  
             // public ActionResult PostDemo(string param1, string param2)
   
              return View( "GetDemo" );
          }
   
          /// <summary>
          /// 处理上传文件的 Action
          /// </summary>
          /// <param name="file1">与传过来的 file 类型的 input 的 name 相对应</param>
          [AcceptVerbs(HttpVerbs.Post)]
          public ActionResult UploadFile(HttpPostedFileBase file1)
          {
              // Request.Files - 获取需要上传的文件。当然,其也会自动映射到同名参数
              // HttpPostedFileBase hpfb = Request.Files[0] as HttpPostedFileBase;
   
              string targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Upload" , Path.GetFileName(file1.FileName));
             file1.SaveAs(targetPath);
   
             return View( "UploadDemo" );
          }
     }
  }
目录
相关文章
|
11天前
|
人工智能 JSON API
全网刷屏的 Jev 模型正式开放!一手实战测评 + 保姆级教程
全网爆火的 Jev 模型是什么?有什么用?怎么使用?怎么接入 AI 编程工具?效果真的好么?傻子可懂的 Jev 保姆级实战教程 + 项目实战测评来啦
7853 14
|
9天前
|
人工智能 测试技术 API
最近全网爆火的 Jev 到底是什么?适合干什么、怎么用,一篇讲透!
Jev是TypeSafe AI推出的“系统一模型”,不生成文本,专做毫秒级结构化决策:Choice(多选)、Score(打分)、Noul(是非概率)。响应快193倍、成本低444倍,适合工单路由、内容审核、测试定级等高频判断场景。
1712 4
最近全网爆火的 Jev 到底是什么?适合干什么、怎么用,一篇讲透!
|
10天前
|
人工智能 并行计算 PyTorch
秋叶 ComfyUI 2026 整合包 v3.2 完整部署教程:Python 3.13 + Torch 2.13 全栈升级
秋叶aaaki ComfyUI 2026年8月整合包v3.2正式发布!全面升级Python 3.13.11、PyTorch 2.13.0+cu130及ComfyUI v0.30.2,原生支持MiniMax H3、Wan 2.2、Qwen-Image-2.1等2026主流音视频/图像模型,解压即用,无需环境配置。
1545 11
|
6天前
|
人工智能 JavaScript 芯片
DeepSeek 官方偷偷上传 Harness 桌面端安装包,我已经用上了。。附最新下载地址
DeepSeek Harness 官方的桌面端安装包被网友扒出来了,2 分钟讲明白如何使用,体验如何,适合作为 AI 编程工具么?附最新 Windows 和 Mac 双端的下载地址
1487 1
|
8天前
|
人工智能 编解码 并行计算
MiniMax-H3 一键整合包技术文档:8G 显存运行 AI 漫剧制作 —— 角色替换 / 动作迁移 / 文图生视频部署与调参指南
MiniMax H3 是 MiniMax 开源的全模态视频生成模型,支持文/图/音/视多条件输入,输出最高2K、15秒带双声道音频视频。本文档详述其Int8量化版在8GB显存下的本地一键部署、三段式工作流(EDIT/REPLACE/CONTINUE)、参数调优及常见问题排查。(239字)
|
23天前
|
人工智能 自然语言处理 安全
阿里云千问办公 QwenWork详细介绍:产品核心能力、典型场景、价格及常见问题解答
千问办公是阿里云推出的一站式AI办公平台,主打"不止于对话,更注重交付",依托通义千问旗舰大模型,用户一句话即可完成数据分析、PPT生成、视频剪辑等复杂任务,直接输出可用成果。产品深度打通钉钉生态与企业OA,覆盖桌面端、网页端,提供企业标准版198元/人/月等多档订阅方案,新用户注册即赠2000积分,适配工程师、HR、财务等多职业办公场景,成为能动手干活的"全能AI同事"。
3755 10
|
18天前
|
缓存 IDE Java
【保姆级】Android Studio下载、安装和汉化教程(2026最新)
Android Studio 是 Google 官方推出的免费 Android 应用开发集成环境,基于 IntelliJ IDEA,内置模拟器、调试器、性能分析及 Compose 界面工具,功能全面,文档丰富,是安卓开发首选工具。(239字)
1914 1