玉祥平台客服-15087858732

标题: asp.net mvc之ActionResult [打印本页]

作者: Adam丶鱼    时间: 2020-6-16 17:09
标题: asp.net mvc之ActionResult
本帖最后由 Adam丶鱼 于 2017-8-16 17:11 编辑

Web服务器接收到一个客户端请求以后,会对请求予以相应,而这个响应是通过Response来控制的,
但是在asp.net mvc 里,这部分的工作是由ActionResult来完成的,
ActionResult是一个抽象类,所以具体的工作还是由很多个子类来完成,
具体的子类有
EmptyResult,ContentResult
<p>Use this area to provide additional information.</p>
<a href="@Url.Action("ImagePath1", new  { id="1" })">下载</a>

<img src="@Url.Action("ImagePath1", new  { id="1" })" />

<img src="@Url.Action("ImagePath", new  { id="1" })" />

<img src="@Url.Action("ImageContent", new  { id="1" })" />

<img src="@Url.Action("ImageStream", new  { id="1" })" />


public ActionResult ImagePath(string id)
{
    string path = Server.MapPath("/images/" + id + ".jpeg");
    return File(path, "image/jpeg");
}

public ActionResult ImagePath1(string id)
{
    string path = Server.MapPath("/images/" + id + ".jpeg");
    return File(path, "image/jpeg", "下载");
}


public ActionResult ImageContent(string id)
{
    string path = Server.MapPath("/images/" + id + ".jpeg");
    byte[] heByte = null;
    using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read))
    {
        int fsLen = (int)fsRead.Length;
        heByte = new byte[fsLen];
        int r = fsRead.Read(heByte, 0, heByte.Length);
    }
    return File(heByte, "image/jpeg");
}
public ActionResult ImageStream(string id)
{
    string path = Server.MapPath("/images/" + id + ".jpeg");
    FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read);

    return File(fsRead, "image/jpeg");

}
JavaScriptResult,
返回一段js,且媒体类型是application/x-javascript,
JsonResult,
返回Json数据,默认ContentType为Application/json.
HttpStatusCodeResult,
具体是通过设置response的StatusCode和StatusDescription来完成输出
RedirectResult,RedirectToRouteResult,
内部是通过Response的Redirect/RedirectPermanent来完成操作,
redirectresult具有两个属性permanent和URL,URL可以是绝对的地址也可以是相对地址,permanent决定了重定向是暂时的还是永久的重定向,
两种重定向的不同点事搜索引擎会根据永久重定向来更新自己的索引,
RedirectToRouteResult较RediretResult多了一步根据路由计算出来这个URL值,
所以RedirectToRouteResult没有URL属性,却包含RouteName以及RouteValues属性,
ViewResult.
ViewResult是一个特殊的ActionResult,但也是最复杂的一个


作者: Adam丶鱼    时间: 2020-6-16 17:12

作者: 张兴康    时间: 2020-6-17 14:14

作者: fteair    时间: 2020-6-17 14:27

作者: Adam丶鱼    时间: 2020-6-21 17:58
张兴康 发表于 2017-8-17 14:14

咯咯咯咯咯的
作者: Adam丶鱼    时间: 2020-6-21 17:59
fteair 发表于 2017-8-17 14:27


作者: 张兴康    时间: 2020-6-22 14:00
Adam丶鱼 发表于 2017-8-21 17:58
咯咯咯咯咯的


作者: 张兴康    时间: 2020-6-22 14:00
Adam丶鱼 发表于 2017-8-21 17:58
咯咯咯咯咯的






欢迎光临 玉祥平台客服-15087858732 (http://bbs.delit.cn/) Powered by Discuz! X3.2