c# - Web API 重定向到网页

标签 c# .net .net-core asp.net-web-api2

我有一个 Web API,我需要将其转换为 API Core 2。我有一个似乎无法解决的问题。 Controller 中有一个使用 Request.CreateResponse 的重定向,它在 .Net Core 中似乎不可用?

public HttpResponseMessage Get() {
    var response = Request.CreateResponse(HttpStatusCode.Redirect);
    response.Headers.Location = new Uri("https://xxx.xxxx.com");
    return response;
}

知道如何修改它才能工作吗?基本上,如果用户不传递任何参数,我们只是将它们发送到网页。

最佳答案

ASP.Net-Core 中的语法发生了变化。 HttpResponseMessage 不再用于操作结果。它们将像任何其他模型返回的对象一样序列化。

改用 Controller.Redirect 方法返回 RedirectResult

public IActionResult Get() {
    return Redirect("https://xxx.xxxx.com");
}

RedirectResult

RedirectResult will redirect us to the provided URL, it doesn’t matter if the URL is relative or absolute, it just redirect, very simple. Other thing to note is that it can redirect us temporarily which we’ll get 302 status code or redirect us permanently which we’ll get 301 status code. If we call the Redirect method, it redirect us temporarily...

引用 Asp.Net Core Action Results Explained

关于c# - Web API 重定向到网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50066417/

相关文章:

c# - 减少到服务器/数据库的往返

c# - 更改文本的 ASP.net 进度条

performance - dotnet core 2 由于恢复时间长而构建时间长

visual-studio - 使用 Visual Studio 2017 调试 NuGet 包

c# - 交易范围和交易

c# - 如何根据 C# 中的枚举值获得优势?

.net - WPF 密码框 : How do I warn a user that Caps Lock is enabled?

c# - 没有指定名称字段的编码,任何非 ASCII 字节都将被丢弃

c# - 如何计算手机平放时绕 y 轴(滚动)的旋转?

c# - 找到 VS 2010 Pro 中未使用的所有对象以便可以安全地删除它们?