在ASP.NET Core MVC控制器中,我们可以使用Response.Headers.Add方法添加一个Content-Type标头,以指定重定向的内容类型。以下是一个示例代码段:
public IActionResult RedirectToMyAction()
{
var url = Url.Action("MyAction", "MyController");
Response.Headers.Add("Content-Type", "text/html");
return Redirect(url);
}
在上面的代码中,我们通过调用Url.Action方法来获取目标动作的URL,并使用Response.Headers.Add方法来添加Content-Type标头。最后,我们使用Redirect方法将请求重定向到目标URL。
请注意,如果我们使用RedirectToAction方法或RedirectToRoute方法来执行重定向,我们可以在方法调用中指定Content-Type标头,如下所示:
public IActionResult RedirectToMyAction()
{
return RedirectToAction("MyAction", "MyController", new { }, "text/html");
}
在上面的代码中,我们使用RedirectToAction方法,同时在方法调用中传递Content-Type标头作为最后一个参数。这样,我们就可以在执行重定向时设置Content-Type标头。