Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am using asp.net web api and I want to send proper status code along with the response. Currently it always returns 200 OK status.

I would like to send different response status like

on POST 201 Record Created
on PUT 205 Record Modified
on PUT 412 Validation Failed
on DELETE 204 Record Deleted etc.

These are quite standard status codes I found and all actions in web api accepts these request types.

I am currently struggling to find a clear example of how, in one place (most probably via action filter or http module) I can add that status code.
Posted
Updated 3-Mar-15 2:47am
v2

This status code is set here: https://msdn.microsoft.com/en-us/library/system.web.httpresponse.statuscode%28v=vs.110%29.aspx[^].

If you want to centralize the code setting the status code, just do it. I see no problem here.

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Mar-15 1:10am    
What event, why? Just create some class handling it, and so on. I don't need it needs anything "in mind".
—SA
XML
<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
<LayoutTemplate>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>
                CustomerId
            </th>
            <th>
                ContactName
            </th>
            <th>
                Country
            </th>
        </tr>
        <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
        <tr>
            <td colspan = "3">
                <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="10">
                    <Fields>
                        <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
                            ShowNextPageButton="false" />
                        <asp:NumericPagerField ButtonType="Link" />
                        <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton = "false" />
                    </Fields>
                </asp:DataPager>
            </td>
        </tr>
    </table>
</LayoutTemplate>
<GroupTemplate>
    <tr>
        <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
    </tr>
</GroupTemplate>
<ItemTemplate>
    <td>
        <%# Eval("CustomerId") %>
    </td>
    <td>
        <%# Eval("ContactName") %>
    </td>
    <td>
        <%# Eval("Country") %>
    </td>
</ItemTemplate>
</asp:ListView>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900