2016-04-19 30 views
0

我要保持我的返回類型爲字符串403個狀態碼,但要響應碼是403如何回報apicontroller行動

這是我現在有。

[HttpGet] 
public string test(string echoText) 
{ 
    // Not authorized 
    if (echoText == "403") 
    { 
     StatusCode(HttpStatusCode.Forbidden); 
     return ""; 
    } 
    else 
     return echoText; 
} 

UPDATE

我發現下面的代碼,但有沒有拋出異常另一種方式?

throw new HttpResponseException(HttpStatusCode.Unauthorized); 

回答

1

你可以拋出一個HttpResponseException你的行動中:

[HttpGet] 
public string test(string echoText) 
{ 
    // Not authorized 
    if (echoText == "403") 
    { 
     throw new HttpResponseException(HttpStatusCode.Forbidden); 
    } 
    else 
     return echoText; 
} 
+0

之前,我剛剛更新了我大約2秒的問題您的文章:) – user3953989

+1

沒看到您的更新,對不起!如果你想讓你的返回類型保持一個字符串,我不相信你有其他選擇。 –