简体   繁体   English

ASP.Net Web API是否在发生异常时返回特定的错误数据?

[英]ASP.Net Web API Return Specifc Error Data on Exception?

.NET 4.5.2, with the following routing information setup: .NET 4.5.2,具有以下路由信息设置:

public void Configuration( IAppBuilder appBuilder )
{
    var conf = new HttpConfiguration();
    conf.Routes.MapHttpRoute(
        name: "DefaultApi" ,
        routeTemplate: "service/2014/{controller}/{appKey}" , 
        defaults: new { appKey = RouteParameter.Optional }
    );
    appBuilder.UseWebApi( conf );
}

And with the following controller code: 并带有以下控制器代码:

public HttpResponseMessage Get( string appKey , string qs1 , string qs2 )
{
    var remess = new HttpResponseMessage { RequestMessage = Request , StatusCode = HttpStatusCode.OK };
    if ( true == new BusinessClass().ValueCheck( appKey , qs1 , qs2 ) )
    {
        remess.Content =  new StringContent( "1" , Encoding.UTF8 , "text/plain");
    }
    else
    {
        remess.Content =  new StringContent( "0" , Encoding.UTF8 , "text/plain");
    }
    return remess;
}

If I use this URI, it properly returns a "0" or a "1" based on the business logic: 如果我使用此URI,则它将根据业务逻辑正确返回“ 0”或“ 1”:

http://localhost:963/service/2014/foo/appgo1?qs1=a&qs2=b

If I use this URI (leaving off the querystring values): 如果我使用此URI(不使用querystring值):

http://localhost:963/service/2014/foo/appgo1

I get a framework-controlled message: 我收到了一个框架控制的消息:

<Error> <Message> No HTTP resource was found that matches the request
URI 'http://localhoost:963/service/2014/foo/appgo1'.
</Message> <MessageDetail> No action was found on the controller
'foo' that matches the request. </MessageDetail> </Error>

For this controller only, I would like to trap the fact that the querystring parameters are wrong and return a -1 instead. 仅对于此控制器,我想捕获querystring参数错误的事实,并返回-1。 There is another controller that does not take querystring parameters at all, as well. 还有另一个控制器也不使用querystring参数。 Can anyone steer me in the right direction on this? 有人可以引导我朝正确的方向前进吗?

Thanks. 谢谢。

This is not the most elegant solution though it does work: 尽管它确实有效,但这并不是最优雅的解决方案:

    public HttpResponseMessage Get(string appKey, string qs1 = null, string qs2 = null)
    {

        var remess = new HttpResponseMessage { RequestMessage = Request, StatusCode = HttpStatusCode.OK };

        if (qs1 == null || qs2 == null)
        {
            remess.Content = new StringContent("-1", Encoding.UTF8, "text/plain");
        }
        else if ( true == new BusinessClass().ValueCheck( appKey , qs1 , qs2 ) )
        {
            remess.Content = new StringContent("1", Encoding.UTF8, "text/plain");
        }
        else
        {
            remess.Content = new StringContent("0", Encoding.UTF8, "text/plain");
        }
        return remess;

    }

You basically make the query string params optional and then check if either is null to return your -1 code. 您基本上将查询字符串参数设为可选,然后检查其中一个是否为null以返回-1代码。 Otherwise, do your business logic checks. 否则,请进行业务逻辑检查。

You could also have a default GET action to catch all gets to the controller and return -1 there. 您还可以使用默认的GET操作来捕获所有到控制器的获取并在那里返回-1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM