上海好程序员 2019-02-07 13:20 采纳率: 0%
浏览 6007

Asp.NET Core RedirectResult 是怎样实现页面跳转的?

首先我定义一个接口,行为是跳转

    [ApiController]
    public class DefaultController : ControllerBase
    {
        [HttpGet("view/{id}")]
        public IActionResult View(int id)
        {
            return Redirect("/view?id=" + id);
        }
    }

然后我定义个页面view.cshtml

@page
@{
    Layout = null;
    var id = Request.QueryString.Value;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>view</title>
</head>
<body>
    Id:  @id
</body>
</html>

1、
我浏览器直接访问 http://localhost:1634/view/1

浏览器地址跳转到 http://localhost:1634/view?id=1

2、
我用postman访问 http://localhost:1634/view/1

拿到的是HTML代码

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>view</title>
    </head>
    <body>
    Id:  ?id=1
</body>
</html>

headers

Transfer-Encoding →chunked
Content-Type →text/html; charset=utf-8
Server →Kestrel
X-SourceFiles →=?UTF-8?B?RDpcU3ZuXHByb2plY3RzLWRvdC1uZXQtY29yZVxNaXJQdWJsaXNoXE1pclB1Ymxpc2hcdmlldw==?=
X-Powered-By →ASP.NET
Date →Thu, 07 Feb 2019 05:14:21 GMT

那么问题来了,既然代码中没有javascript.location.href=也没有meta.refresh.url=浏览器是怎么实现的地址重定位?

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-02-07 13:25
    关注

    服务器会返回给客户端一个302的跳转,其中的location字段就告诉浏览器,要跳转到这个地址。这些信息不是在html/js里,而是在http响应头里。

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效