0
我正在製作一個Web API後端,它需要返回當前登錄用戶的名稱和角色。雖然這個作品中,問題是,我返回OK(「一些字符串」)函數返回是這樣的:WebAPI 2:如何使用IHttpActionResult返回一個原始字符串?
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Current user: MyUser Role: MyRole</string>
這裏是我的代碼:
[ResponseType(typeof(string))]
public IHttpActionResult GetCurrentUser()
{
if (User.Identity.IsAuthenticated)
{
string role = service.GetRole(User.Identity.Name);
if (role != null)
{
return Ok("Current user: " + User.Identity.Name + " " + "Role: " + role);
}
else
{
return Ok("Current user: " + User.Identity.Name + " " + "Role: " + "No role assigned");
}
}
else
{
return BadRequest("Not authenticated");
}
}
我怎樣才能讓這個回報只是
當前用戶:MyUser角色:MyRole?
爲什麼[ResponseType(typeof(string))]?只要回覆回覆,你是否使用郵遞員進行測試? –
這是爲我的幫助頁面,所以做前端的人應該知道什麼類型的響應應該期望。它與回報實際返回無關。 – Artyomska
https://stackoverflow.com/questions/37492010/asp-net-mvc-api-returning-this-xml-file-does-not-appear-to-have-any-style-infor任何幫助? –