简体   繁体   English

http状态代码200返回预期结果。 但是,当状态为BadRequest或未经授权时,我的C#webapi 1返回text / html

[英]http status code 200 returns expected result. But when status is BadRequest or Unauthorized, my C# webapi 1 is returning text/html

public HttpResponseMessage Get()
    {
        sessionResponse.message = "Not permitted";
        sessionResponse.code = statusCode.Unauthorized;

        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Unauthorized, sessionResponse);
        return response;
    }

This is giving me total html page: 这给了我整个html页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>401 - Unauthorized: Access is denied due to invalid credentials.</title></head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>401 - Unauthorized: Access is denied due to invalid credentials.</h2>
<h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3>
 </fieldset></div>
</div>
</body>
</html>

But when I am running this code: 但是当我运行这段代码时:

public HttpResponseMessage Get()
    {
        sessionResponse.message = "Not permitted";
        sessionResponse.code = statusCode.Unauthorized;

        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, sessionResponse);
        return response;
    }

I am getting desired JSON: { message: "Not permitted", code:"401" } 我正在获取所需的JSON:{消息:“不允许”,代码:“ 401”}

So, I am facing the problem when response is not HTTPSTATUSCODE.OK, I am not getting JSON. 所以,当响应不是HTTPSTATUSCODE.OK时,我遇到了问题,我没有得到JSON。 If status code is Bad request or Unauthorized, I am getting text/html. 如果状态代码是“错误请求”或“未授权”,则我正在获取text / html。 What to do with it ? 怎么办呢? I have added JSON formatter in apiconfig file, but no luck. 我在apiconfig文件中添加了JSON格式化程序,但是没有运气。

您可以使用Request.CreateResponse重载 ,该重载通过string接受MediaTypeFormatter类型或媒体类型,以明确声明您要使用的格式化程序:

return Request.CreateResponse(HttpStatusCode.Unauthorized, sessionResponse, "application/json");

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

相关问题 C#/。NET webapi-从具有列表返回类型的控制器中的方法返回异常(HTTP状态代码) - C#/.NET webapi - Returning an exception (HTTP Status Code) From Method in Controller with List Return Type C#-httpwebrequest状态代码返回200而不是404 - c# - httpwebrequest status code returns 200 instead of 404 HTTP状态代码为401时,IErrorHandler返回错误的消息正文 - IErrorHandler returning wrong message body when HTTP status code is 401 Unauthorized 发生异常时,Web Api始终返回http状态代码200 - Web Api always returns http status code 200 when an exception occurs C# 发布到 asp.net webapi Apiexception“响应的 HTTP 状态代码未被排除 (201)。” - C# post to asp.net webapi Apiexception "The HTTP status code of the response was not excepted (201)." HTTP状态码200,但访问被拒绝。 WebAPI ASP.NET MVC - HTTP status code 200 but access denied. WebAPI ASP.NET MVC 当我在winforms C#中使用web api 发布数据时的状态码200 - Status code 200 when i post data using web api in winforms C# 在C#中获取HTTP状态代码 - Fetch HTTP status code in C# WebApi:在 ApiController 之外创建自定义 BadRequest 状态 - WebApi: Creating custom BadRequest status outside of ApiController 未经授权的消息,状态为200 OK - Unauthorized Message,Status 200 OK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM