简体   繁体   English

如何在 ASP.NET Core MVC controller 中为重定向设置 Content-Type

[英]How to set Content-Type for a Redirect in ASP.NET Core MVC controller

I'm trying to redirect to an external page that accepts application/json content-type using我正在尝试使用重定向到接受 application/json 内容类型的外部页面

return RedirectPermanent(url);

I couldn't find a way to set the cotent-type on RedirectResult but looks like ContentResult could be used this way我找不到在 RedirectResult 上设置 cotent-type 的方法,但看起来ContentResult可以这样使用

var contentResult = new ContentResult();
contentResult.ContentType = "application/json";
contentResult.StatusCode = (int)HttpStatusCode.MovedPermanently;
return contentResult;

How can I set the URL in the ContentResult or set the content-type in RedirectResult?如何在ContentResult中设置 URL 或在 RedirectResult 中设置 content-type?

Setting the URL on Response like this worked像这样在响应上设置 URL 有效

   public ActionResult Endpoint()
   {
        Response.Redirect(url);

        var contentResult = new ContentResult();
        contentResult.ContentType = "application/json";
        contentResult.StatusCode = (int)HttpStatusCode.MovedPermanently;
        return contentResult;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 ASP.NET MVC 上将 Content-Type 设置为完全“文本/纯文本”? - How to set Content-Type to exactly “text/plain” on ASP.NET MVC? ASP.Net Core 为 JSON 内容设置 Content-Type 和 Location 标头 - ASP.Net Core set Content-Type and Location headers for JSON content ASP.NET 核心 2 - 缺少内容类型边界 - ASP.NET Core 2 - Missing content-type boundary InvalidOperationException:错误的内容类型:ASP.NET Core - InvalidOperationException: Incorrect Content-Type: ASP.NET Core ASP.NET MVC应用程序不输出内容类型标头 - ASP.NET MVC Application not outputting content-type header 如何在 ASP.NET MVC 中使用 GET 请求发送 Content-Type 标头? - How can I send Content-Type header with a GET request in ASP.NET MVC? ASP.NET Core 中内容类型“application/csp-report”的“415 Unsupported Media Type” - "415 Unsupported Media Type" for Content-Type "application/csp-report" in ASP.NET Core 尝试使用Ajax将png上载到Asp.net Core服务器时出现“错误的Content-Type:image / png” - “Incorrect Content-Type: image/png” when trying to upload png with Ajax to Asp.net Core server ASP.NET 核心 jQuery POST - 尽管标题格式正确,但内容类型不正确 - ASP.NET Core jQuery POST - Incorrect Content-Type despite correctly formatted headers 如何在 ASP.NET Core 中将控制器重定向到剃刀页面 - How to Redirect Controller to razor page in ASP.NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM