C# ASP.Net Web API在异常时返回指定的错误数据?

C# ASP.Net Web API在异常时返回指定的错误数据?,c#,asp.net,asp.net-mvc,asp.net-web-api,httpresponse,C#,Asp.net,Asp.net Mvc,Asp.net Web Api,Httpresponse,.NET 4.5.2,具有以下路由信息设置: public void Configuration( IAppBuilder appBuilder ) { var conf = new HttpConfiguration(); conf.Routes.MapHttpRoute( name: "DefaultApi" , routeTemplate: "service/2014/{controller}/{appKey}" , def

.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 );
}
并具有以下控制器代码:

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;
}
如果使用此URI,它会根据业务逻辑正确返回“0”或“1”:

http://localhost:963/service/2014/foo/appgo1?qs1=a&qs2=b
如果使用此URI(不使用querystring值):

我收到一条框架控制的消息:

<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>
未找到与请求匹配的HTTP资源
URI'http://localhoost:963/service/2014/foo/appgo1'.
在控制器上找不到任何操作
与请求匹配的“foo”。
仅对于这个控制器,我想捕捉querystring参数错误的事实,并返回-1。还有一个控制器根本不接受querystring参数。谁能在这件事上给我指引正确的方向


谢谢。

这不是最优雅的解决方案,尽管它确实有效:

    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;

    }
基本上,将查询字符串params设置为可选,然后检查其中一个是否为null以返回-1代码。否则,请执行业务逻辑检查

您还可以有一个默认的GET操作来捕获到控制器的所有GET并在那里返回-1

    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;

    }