本文介绍了在Web API中,当一种方法包含以可空值类型作为参数的方法时,如何重载Get的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新

我最初的假设是可选参数是问题的原因.这似乎是不正确的.相反,当多个操作方法之一包含某些参数的可空值类型(例如int?)时,这似乎是一个问题.

My original assumption was that optional parameters were the cause of the problem. That appears to be incorrect. Instead, it appears to be a problem with multiple action methods when one of those methods contains nullable value types (e.g. int? ) for some of the parameters.

我正在使用Visual Studio 2012 RC,并且刚刚开始使用Web API.我遇到了一个问题,并收到错误消息找不到与请求匹配的控制器'Bars'上的操作."

I'm using Visual Studio 2012 RC and am just getting started with Web API. I've run into an issue and getting the error "No action was found on the controller 'Bars' that matches the request."

我有一个Bars控制器.它具有一个Get()方法,该方法接受可选参数.

I've got a Bars controller. It has a Get() method that takes in optional parameters.

public IEnumerable<string> Get(string h, string w = "defaultWorld", int? z=null)
{
    if (z != 0)
        return new string[] { h, w, "this is z: " + z.ToString() };
    else
       return new string[] { h, w };
}

因此,我使用以下网址对其进行了测试

So, I test it out with the following urls

  • /api/bars?h = hello
  • /api/bars?h = hello& w = world
  • /api/bars?h = hello& w = world& z = 15

它对所有三个都有效.

然后,我要添加另一个Get()方法,这次仅使用一个id参数

Then, I go to add another Get() method, this time with a single id parameter

 public string Get(int id)
 {
     return "value";
 }

我再次测试了网址.这次/api/bars?h = hello& w = world和api/bars?h = hello失败.错误消息为在与请求相匹配的控制器'Bar'上未找到操作."

I test the urls again. This time /api/bars?h=hello&w=world and api/bars?h=hello fail. The error message is "No action was found on the controller 'Bar' that matches the request."

由于某种原因,这两种方法不能很好地配合使用.如果我删除 Get(int id),它可以工作.如果我更改int?将z转换为字符串z,然后它可以工作(但随后需要在我的action方法中转换对象!).

For some reason, these two methods don't play nicely together. If I remove Get(int id), it works. If I change int? z to string z, then it works (, but then it requires converting the objects inside my action method!).

为什么Web API会这样做?这是错误还是故意的?

Why is Web API doing this? Is this a bug or by design?

非常感谢.

推荐答案

问题已解决,但是还存在另一个问题.问题似乎在于,重载的Action方法的可选参数有问题.

Problem solved, although, it leaves an additional question. The problem appears to be that the overloaded Action methods are having problems with the optional parameters.

所以新的问题是为什么要这样做,但是我会把这个问题留给比我低的人;)

So the new question is why so, but I will leave that up to lower level guys than me ;)

但这是个好消息.我不喜欢您所报告的问题,虽然很高兴知道,但是走复杂类型路由只是杰里(Jerry)钻机修复,对Web Api的工作方式反映得很差.因此,好消息是,如果您有此问题,则可以通过简单地取消可选参数来解决,并执行良好的重载路线即可解决.好消息是,这绝不是杰里(Jerry)钻机修复,只会使您失去一些可选参数的便利性:

But this is good news. I didn't like the problem you reported, and going the complex type route, while nice to know, is simply a jerry rig fix and would reflect very poorly on how something is working in the Web Api. So the good news is, if you have this problem, it is solved by simply doing away with the optional params, do the good ol' overloads route. Good news, as this is by no means a jerry rig fix, simply makes you loose a little optional parameter convenience:

public class BarsController : ApiController
{
    public string Get(int id)
    {
        return "value";
    }

    public IEnumerable<string> Get(string h)
    {
        return Get(h, null, null);
    }

    public IEnumerable<string> Get(string h, string w)
    {
        return Get(h, w, null);
    }

    public IEnumerable<string> Get(string h, string w, int? z) 
    {
        if (z != 0)
            return new string[] { h, w, "this is z: " + z.ToString() };
        else
            return new string[] { h, w };
    }
}

欢呼

这篇关于在Web API中,当一种方法包含以可空值类型作为参数的方法时,如何重载Get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:24