IHttpActionResult 使用範例

舊的作法
public HttpResponseMessage Delete(int id)
{
    var status = _Repository.DeleteCustomer(id);
    if (status)
    {
        return new HttpResponseMessage(HttpStatusCode.OK);
    }
    else
    {
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }
}

Web API 2 作法
public IHttpActionResult SomeAction()
{
   IHttpActionResult response;
   //we want a 303 with the ability to set location
   HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.RedirectMethod);
   responseMsg.Headers.Location = new Uri("http://customLocation.blah");
   response = ResponseMessage(responseMsg);
   return response;
}

留言

這個網誌中的熱門文章

[C#]Windows 10 停用與啟用網路卡(連線)[手把手教程][原創]

ASP.NET Web API 將傳回的值轉換從控制器動作至 HTTP 回應訊息的方式。

[C#]程式更改電腦IP位置與電腦名稱