将自定义状态代码添加到asp.net mvc web api中的所有响应中 [英] Add custom status code to all responses in asp.net mvc web api

查看:65
本文介绍了将自定义状态代码添加到asp.net mvc web api中的所有响应中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.net web api,我想发送正确的状态代码以及响应。目前它总是返回200 OK状态。



我想发送不同的响应状态,如

on POST 201记录创建了PUT 205上的
PUT 412上的记录修改
验证失败了
删除204记录已删除等等。



这些是我找到了非常标准的状态代码,web api中的所有操作都接受这些请求类型。



我目前正努力在一个地方(大多数地方)找到一个明确的例子可能通过动作过滤器或http模块)我可以添加该状态代码。

解决方案

此状态代码在此处设置:https://msdn.microsoft.com/en-us/library/system.web.httpresponse .statuscode%28v = vs.110%29.aspx [ ^ ] 。



如果你想集中设置状态代码的代码,就这样做吧。我觉得这里没问题。



-SA


 <   asp:ListView     ID   =  lvCustomers    runat   = 服务器    GroupPlaceholderID   =  groupPlaceHolder1  

ItemPlaceholderID = itemPlaceHolder1 < span class =code-attribute> OnPagePropertiesChangin g = OnPagePropertiesChanging >
< LayoutTemplate >
< table cellpadding = 0 cellspacing = 0 >
< tr >
< th >
CustomerId
< / th >
< th >
ContactName
< / th >
< th >
国家/地区
< / th >
< / tr >
< ASP:占位符 runat = 服务器 ID = groupPlaceHolder1 > < / asp:PlaceHolder >
< tr >
< td colspan = 3 >
< asp:DataPager ID = DataPager1 runat = server PagedControlID = lvCustomers PageSize = 10 >
< 字段 >
< asp:NextPreviou sPagerField ButtonType = 链接 ShowFirstPageButton = false ShowPreviousPageButton = true

ShowNextPageButton = false / >
< asp:NumericPagerField ButtonType = 链接 / >
< asp:NextPreviousPagerField ButtonType = 链接 ShowNextPageButton = true ShowLastPageButton = false ShowPreviousPageButton = false / >
< / Fields >
< / asp:DataPager >
< span class =code-keyword><
/ td >
< / tr >
< / table >
< / Layo utTemplate >
< GroupTemplate >
< tr >
< asp:PlaceHolder runat = server ID = itemPlaceHolder1 > < / asp:PlaceHolder >
< / tr >
< / GroupTemplate >
< ItemTemplate >
< td >
<% #Eval( CustomerId%>
< span class =code-keyword><
/ td >
< td >
<% #Eval( ContactName%>
< < span class =code-leadattribute> / td >
< td >
<% #Eval( Country%> ;
< / td >
< / ItemTemplate >
< / asp:ListView >


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.

解决方案

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


<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>


这篇关于将自定义状态代码添加到asp.net mvc web api中的所有响应中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆