1.在Action处理之后,必须有一个返回值,这个返回值必须继承自ActionResult的对象

2.ActionResult,实际就是服务器端响应给客户端的结果

  • ViewResult(返回视图结果)
  • FileResult(二进制文件相应给客户端),FileContentResult继承自FileResult
  • ContentResult(将内容相应给客户端)
  • JsonResult(将对象传递给客户端)
  • JavaScriptResult(将一段JavaScript脚本传递给客户端)
  • PartiaView(返回分布视图)

FileContentResult的应用案例

做一个验证码图片

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging; namespace MvcBookShop.Common
{
public class ValidateCode
{
/// <summary>
/// 生成验证码
/// </summary>
/// <param name="length">指定验证码的长度</param>
/// <returns></returns>
public string CreateValidateCode(int length)
{
int[] randMembers = new int[length];
int[] validateNums = new int[length];
string validateNumberStr = "";
//生成起始序列值
int seekSeek = unchecked((int)DateTime.Now.Ticks);
Random seekRand = new Random(seekSeek);
int beginSeek = (int)seekRand.Next(, Int32.MaxValue - length * );
int[] seeks = new int[length];
for (int i = ; i < length; i++)
{
beginSeek += ;
seeks[i] = beginSeek;
}
//生成随机数字
for (int i = ; i < length; i++)
{
Random rand = new Random(seeks[i]);
int pownum = * (int)Math.Pow(, length);
randMembers[i] = rand.Next(pownum, Int32.MaxValue);
}
//抽取随机数字
for (int i = ; i < length; i++)
{
string numStr = randMembers[i].ToString();
int numLength = numStr.Length;
Random rand = new Random();
int numPosition = rand.Next(, numLength - );
validateNums[i] = Int32.Parse(numStr.Substring(numPosition, ));
}
//生成验证码
for (int i = ; i < length; i++)
{
validateNumberStr += validateNums[i].ToString();
}
return validateNumberStr;
} /// <summary>
/// 创建验证码的图片
/// </summary>
/// <param name="containsPage">要输出到的page对象</param>
/// <param name="validateNum">验证码</param>
public byte[] CreateValidateGraphic(string validateCode)
{
Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), );
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的干扰线
for (int i = ; i < ; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", , (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(, , image.Width, image.Height),
Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(validateCode, font, brush, , );
//画图片的前景干扰点
for (int i = ; i < ; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), , , image.Width - , image.Height - );
//保存图片数据
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
//输出图片流
return stream.ToArray();
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
}

ValidateCode

 //创建图片验证码
public ActionResult ValidateImage()
{
ValidateCode validateCode=new ValidateCode ();
string strCode=validateCode.CreateValidateCode();
Session["validateCode"]=strCode;
Byte[] ImageCode=validateCode.CreateValidateGraphic(strCode);
return File(ImageCode, @"image/gif");//类似于Content-Type,表示服务器响应给服务器端的类型
}
//表示返回的是FileContentResult对象,ImageCode表示一个二进制的字节数组,@"image/gif"表示服务器相应给客户端的二进制文件类型。

JsonResult案例,实际是把对象序列化后发给客户端。(这个对象一定要重新构建对象的序列图,实际就是需要new一个新的对象出来)

在用户登陆之后鼠标移动在上面有个悬浮窗显示用户的基本信息

<div id="userinfo" style="display:none">
<table border="">
<tr>
<th>
用户名
</th>
<th>
地址
</th>
<th>
电话
</th>
</tr>
<tr>
<td id="Name"> </td>
<td id="Address"> </td>
<td id="Phone"> </td>
</tr>
</table>
</div>

悬浮层div代码

<script language="javascript" type="text/javascript">
$(function () {
$.get(("/User/ShowUser"), null, function (data) {
if (data != "") {
$("#loginUser").text(data).attr("href", "#").attr("iflogin", "true");
}
}, "text"); $("#loginUser").mouseover(function () { if ($(this).attr("iflogin")) {
$.post("/User/GetUserInfo", null, function (data) {
$("#Name").html(data.Name);
$("#Address").html(data.Address);
$("#Phone").html(data.Phone);
$("#userinfo").css("display", "block");
}, "json");
} else {
$(this).unbind("mouseover"); //将某个事件操作移除
}
}).mouseout(function () {
$("#userinfo").css("display", "none");
}); });
</script>

js代码

public ActionResult ShowUser()
{
ContentResult content = new ContentResult();
if (Session["User"] == null)
{
content.Content = "";
}
else
{
content.Content = (Session["User"] as MvcBookShop.Models.User).Name;
}
return content;
}
[HttpPost]
public ActionResult GetUserInfo()
{
JsonResult js = new JsonResult();
MvcBookShop.Models.User user = Session["User"] as MvcBookShop.Models.User;
//构建新的对象序列图,必须要new
var obj = new
{
Name=user.Name,
Address=user.Address,
Phone=user.Phone
};
js.Data = obj;
//如果这个请求非要是get请求那么需要加下面这段代码
//js.JsonRequestBehavior = JsonRequestBehavior.AllowGet; //允许来自客户端的get请求
return js;
}

注意:在请求JsonResult对象返回的时候,一般默认都采用Post方式请求,如果非要get方式请求,需要去加一段代码:设置:JsonRequestBehavior属性值为:JsonRequestBehavior.AllowGet;

[HttpGet]
public ActionResult GetUserInfo()
{
JsonResult js = new JsonResult();
MvcBookShop.Models.User user = Session["User"] as MvcBookShop.Models.User;
//构建新的对象序列图,必须要new
var obj = new
{
Name=user.Name,
Address=user.Address,
Phone=user.Phone
};
js.Data = obj;
//如果这个请求非要是get请求那么需要加下面这段代码
js.JsonRequestBehavior = JsonRequestBehavior.AllowGet; //允许来自客户端的get请求
return js;
}

Get请求时的代码写法

9.MVC框架开发(关于ActionResult的处理)的更多相关文章

  1. ASP.NET MVC框架开发系列课程 (webcast视频下载)

    课程讲师: 赵劼 MSDN特邀讲师 赵劼(网名“老赵”.英文名“Jeffrey Zhao”,技术博客为http://jeffreyzhao.cnblogs.com),微软最有价值专家(ASP.NET ...

  2. 2.MVC框架开发(视图开发----基础语法)

    1.区别普通的html,在普通的html中不能将控制器里面的数据展示在html中. 在MVC框架中,它提供了一种视图模板(就是结合普通的html标签并能将控制器里传出来的数据进行显示) 视图模板特性: ...

  3. 10.MVC框架开发(Ajax应用)

    1.MVC自带的Ajax应用, 使用步骤: 第一步,引入js框架 <script src="../../Scripts/jquery-1.4.4.js" type=" ...

  4. 5.MVC框架开发(强类型开发,控制器向界面传递数据的几种方法)

    界面表单中的表单元素名字和数据库表的字段名相一一映射(需要哪个表的数据就是那个表的模型(Model)) 在View页面中可以指定页面从属于哪个模型 注:以上的关系可以通过MVC的强类型视图开发来解决我 ...

  5. 1.MVC框架开发(初识MVC)

    1.约定大于配置 Content:存放静态文件(样式表.静态图片等) Controllers:存放控制器类 Models:存放数据模型文件 Scripts:存放脚本文件 Views:存放视图文件,里面 ...

  6. 8.MVC框架开发(URL路由配置和URL路由传参空值处理)

    1.ASP.NET和MVC的路由请求处理 1)ASP.NET的处理 请求---------响应请求(HttpModule)--------处理请求(HttpHandler)--------把请求的资源 ...

  7. 4.MVC框架开发(母版页的应用、按钮导致的Action处理、从界面向控制器传数据和HtmlHelper控件的实现(注册的实现))

    1.在视图里如何引入母版页 1)在视图里母版页都是放在View目录下面的Shared文件夹下面 2)母版页里的RenderBody()类似于ASP.NET里面的ContentPalceHolder占位 ...

  8. 了解MVC框架开发

    版权声明:本文为博主原创文章,未经博主允许不得转载. 前言:本篇文章我们浅谈下MVC各个部分,模型(model)-视图(view)-控制器(controller), 以及路由. 对于使用MVC的好处大 ...

  9. 7.MVC框架开发(创建层级项目)

    在一个项目比较大的时候,就会有多个层级项目 1)在项目中选定项目右建新建区域(新的层级项目),项目->右键->添加->区域,构成了一套独立的MVC的目录,这个目录包括Views,Co ...

随机推荐

  1. Database ORM

    Database ORM Introduction Basic Usage Mass Assignment Insert, Update, Delete Soft Deleting Timestamp ...

  2. Making Use of Forms and Fieldsets

    Making Use of Forms and Fieldsets So far all we have done is read data from the database. In a real- ...

  3. Android_listView_Listener

    layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  4. mkfs.xfs命令没找到

    yum install xfsprogs xfsdump

  5. 关于apple watch(苹果表)

      如何升级呢? 对于Apple Watch用户来说,只要打开Apple Watch的iPhone应用,打开主菜单然 后选择软件升级,就能下载升级文件.新版本可以无线安装.需要注意的是,在升级 wat ...

  6. BSP模型

    http://www.uml.org.cn/yunjisuan/201212191.asp Hama中最关键的就是BSP(Bulk Synchronous Parallel-"大型" ...

  7. C#学习笔记14:面向对象继承的特点和里氏转换

    面向对象: 继承:减少代码. 单根性  传递性 子类没有从父类那里继承了构造函数,只是会默认的调用父类那个无参数的构造函数 Class person { Public String Name { Ge ...

  8. c#局域网文件搬移

    /// kongxiang--2013.7.23 /// using System;using System.Collections.Generic;using System.Linq;using S ...

  9. ###《Effective STL》--Chapter5

    点击查看Evernote原文. #@author: gr #@date: 2014-09-17 #@email: forgerui@gmail.com Chapter5 算法 Topic 30: 确保 ...

  10. 我的第一个html页面

    <!DOCTYPE html><meta charset="UTF-8"><title>第一个html5界面</title>< ...