控制器的常用方法

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 MvcRazorDemo
{
public class DemoController : Controller
{ /// <summary>
/// http://localhost:1847/Demo/ContentResultDemo
/// </summary>
/// <returns></returns>
public ActionResult ContentResultDemo()
{
string contentString = "ContextResultDemo! 请查看 Controllers/DemoController.cs文件,里面包含所有类型ActionResult的用法.";
return Content(contentString);
} /// <summary>
/// http://localhost:1847/Demo/EmptyResultDemo
/// </summary>
/// <returns></returns>
public ActionResult EmptyResultDemo()
{
return new EmptyResult();
} /// <summary>
/// http://localhost:1847/Demo/FileContentResultDemo
/// </summary>
/// <returns></returns>
public ActionResult FileContentResultDemo()
{
//创建一个文件流
FileStream fs = new FileStream(Server.MapPath(@"/Content/a.jpg"), FileMode.Open, FileAccess.Read); //定义一个buffer数组
byte[] buffer = new byte[Convert.ToInt32(fs.Length)]; fs.Read(buffer, , Convert.ToInt32(fs.Length) ); return File(buffer, @"image/gif");
} /// <summary>
/// http://localhost:1847/Demo/FilePathResultDemo
/// </summary>
/// <returns></returns>
public ActionResult FilePathResultDemo()
{
//可以将一个jpg格式的图像输出为gif格式
return File(Server.MapPath(@"/Content/a.jpg"), @"image/gif");
} /// <summary>
/// http://localhost:1847/Demo/FileStreamResultDemo
/// </summary>
/// <returns></returns>
public ActionResult FileStreamResultDemo()
{
FileStream fs = new FileStream(Server.MapPath(@"/Content/a.jpg"), FileMode.Open, FileAccess.Read);
return File(fs, @"image/gif");
} /// <summary>
/// http://localhost:1847/Demo/HttpUnauthorizedResultDemo
/// </summary>
/// <returns></returns>
public ActionResult HttpUnauthorizedResultDemo()
{
//返回一个未验证的 401
return new HttpUnauthorizedResult();
} /// <summary>
/// http://localhost:1847/Demo/JavaScriptResultDemo
/// </summary>
/// <returns></returns>
public ActionResult JavaScriptResultDemo()
{
return JavaScript(@"alert(""Test JavaScriptResultDemo!"")");
} /// <summary>
/// http://localhost:1847/Demo/JsonResultDemo
/// </summary>
/// <returns></returns>
public ActionResult JsonResultDemo()
{
var tempObj = new { Controller = "DemoController", Action = "JsonResultDemo" }; return Json(tempObj,JsonRequestBehavior.AllowGet);
} /// <summary>
/// http://localhost:1847/Demo/RedirectResultDemo
/// </summary>
/// <returns></returns>
public ActionResult RedirectResultDemo()
{
return Redirect(@"http://localhost:1847/Demo/ContentResultDemo");
} /// <summary>
/// http://localhost:1847/Demo/RedirectToRouteResultDemo
/// </summary>
/// <returns></returns>
public ActionResult RedirectToRouteResultDemo()
{
return RedirectToAction(@"FileStreamResultDemo");
} /// <summary>
/// http://localhost:1847/Demo/PartialViewResultDemo
/// </summary>
/// <returns></returns>
public ActionResult PartialViewResultDemo()
{
return PartialView();
} /// <summary>
/// http://localhost:1847/Demo/ViewResultDemo
/// </summary>
/// <returns></returns>
public ActionResult ViewResultDemo()
{
//如果没有传入View名称, 默认寻找与Action名称相同的View页面.
return View();
} }
}

MVC控制器常用方法返回类型的更多相关文章

  1. MVC控制器方法返回类型

    控制器公开控制器操作.操作是控制器上的方法,在浏览器的地址栏中输入特定 URL 时被调用.例如,假设要请求下面的 URL: http://localhost/Product/Index/3 在这种情况 ...

  2. Spring MVC之Action返回类型

    Spring MVC支持的方法返回类型 1)ModelAndView 对象.包含Model和View对象,可以通过它访问@ModelAttribute注解的对象. 2)Model 对象.仅包含数据访问 ...

  3. asp.net MVC控制器中返回JSON格式的数据时提示下载

    Asp.net mvc在接收的是JSON格式的数据,但是奇怪的是在IE中提示下载文件,其他浏览器中一切正常,下载后,里面的内容就是在控制器中返回的数据.代码如下: 视图中js代码: $("# ...

  4. 在IE中MVC控制器中返回JSON格式的数据时提示下载

    最近做项目时,视图中用jquery.form.js异步提交表单时,接收的是JSON格式的数据,但是奇怪的是在IE中提示下载文件,其他浏览器中一切正常,下载后,里面的内容就是在控制器中返回的数据.代码如 ...

  5. .net mvc 获取acion 返回类型

    1..net core 中获取 public override void OnActionExecuted(ActionExecutedContext context) { var descripto ...

  6. Spring MVC控制器方法参数类型

    HttpServletRequest Spring会自动将 Servlet API 作为参数传过来 HttpServletResponse InputStream 相当于request.getInpu ...

  7. MVC中FileResult 返回类型返回Excel

    公司中以前写的导出有问题.原来使用的XML格式字符串拼接然后转化成流输出 action public FileResult ExportJobFair() { try { string name = ...

  8. ASP.NET Core 入门教程 4、ASP.NET Core MVC控制器入门

    一.前言 1.本教程主要内容 ASP.NET Core MVC控制器简介 ASP.NET Core MVC控制器操作简介 ASP.NET Core MVC控制器操作简介返回类型简介 ASP.NET C ...

  9. ASP.NET Core 入门笔记5,ASP.NET Core MVC控制器入门

    摘抄自https://www.cnblogs.com/ken-io/p/aspnet-core-tutorial-mvc-controller-action.html 一.前言 1.本教程主要内容 A ...

随机推荐

  1. 对只转发结果集的无效操作 first

    今天只用jdbc连接Oracle查询结果时,出现了一个: 对只转发结果集的无效操作 first 的错误java.sql.sqlexception. 出现这个结果的原因是:使用 Statement st ...

  2. Codeforces Round #216 (Div. 2)A. Valera and Plates

    #include <iostream> using namespace std; int main(){ int n, m , k; cin >> n >> m & ...

  3. POJ-A Simple Problem with Integers

    Description 给出了一个序列,你需要处理如下两种询问. "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000). " ...

  4. js判断访问来源

    通过navigator的userAgent属性来判定 userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值.一般来讲,它是在 navigator.appCode ...

  5. 【BZOJ】2693: jzptab

    http://www.lydsy.com/JudgeOnline/problem.php?id=2693 题意:求$\sum_{i=1}^{n} \sum_{j=1}^{m} lcm(i, j)$, ...

  6. BZOJ1025: [SCOI2009]游戏

    Description windy学会了一种游戏.对于1到N这N个数字,都有唯一且不同的1到N的数字与之对应.最开始windy把数字按顺序1,2,3,……,N写一排在纸上.然后再在这一排下面写上它们对 ...

  7. C#读取XML文件的方法

    先写一个xml文件: <?xml version="1.0" encoding="utf-8" ?> <bookste> <!-- ...

  8. html中隐藏域hidden的作用介绍及使用示例

    基本语法: <input type="hidden" name="field_name" value="value"> 作用:  ...

  9. mvc正则@符号js报错解决办法

    很简单在@前面再加个@就行了,也可以以引进js 的形式解决!

  10. c# winform进入窗口后在文本框里的默认焦点

    c# winform 设置winform进入窗口后在文本框里的默认焦点   进入窗口后默认聚焦到某个文本框,两种方法: ①设置tabindex 把该文本框属性里的tabIndex设为0,焦点就默认在这 ...