fromfire2
上海好程序员
2019-02-07 13:20

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条回答