多个动作被发现匹配的要求,即 [英] Multiple actions were found that match the request

查看:112
本文介绍了多个动作被发现匹配的要求,即的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读了很多有关路由和控制器的问题,但我根本无法找到我要找的。我有这个控制器具有这种结构:

I have read a lot of questions about routing and controllers, but I simply can't find what I'm looking for. I have this controller which has this structure:

包括完整的类源[/编辑]

public class LocationsController : ApiController
{
    private readonly IUnitOfWork _unitOfWork;

    public LocationsController(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    // GET /api/locations/id
    public Location Get(Guid id)
    {
        return this.QueryById<Location>(id, _unitOfWork);
    }

    // GET /api/locations
    public IQueryable<Location> Get()
    {
        return this.Query<Location>(_unitOfWork);
    }

    // POST /api/locations
    public HttpResponseMessage Post(Location location)
    {
        var id = _unitOfWork.CurrentSession.Save(location);
        _unitOfWork.Commit();

        var response = Request.CreateResponse<Location>(HttpStatusCode.Created, location);
        response.Headers.Location = new Uri(Request.RequestUri, Url.Route(null, new { id }));

        return response;
    }

    // PUT /api/locations
    public Location Put(Location location)
    {
        var existingLocation = _unitOfWork.CurrentSession.Query<Location>().SingleOrDefault(x => x.Id == location.Id);

        //check to ensure update can occur
        if (existingLocation == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        //merge detached entity into session
        _unitOfWork.CurrentSession.Merge(location);
        _unitOfWork.Commit();

        return location;
    }

    // DELETE /api/locations/5
    public HttpResponseMessage Delete(Guid id)
    {
        var existingLocation = _unitOfWork.CurrentSession.Query<Location>().SingleOrDefault(x => x.Id == id);

        //check to ensure delete can occur
        if (existingLocation != null)
        {
            _unitOfWork.CurrentSession.Delete(existingLocation);
            _unitOfWork.Commit();
        }

        return new HttpResponseMessage(HttpStatusCode.NoContent);
    }

    // rpc/locations
    public HttpResponseMessage Dummy()
    {
        // I use it to generate some random data to fill the database in a easy fashion
        Location location = new Location();
        location.Latitude = RandomData.Number.GetRandomDouble(-90, 90);
        location.Longitude = RandomData.Number.GetRandomDouble(-180, 180);
        location.Name = RandomData.LoremIpsum.GetSentence(4, false);

        var id = _unitOfWork.CurrentSession.Save(location);
        _unitOfWork.Commit();

        var response = Request.CreateResponse<Location>(HttpStatusCode.Created, location);
        response.Headers.Location = new Uri(Request.RequestUri, Url.Route(null, new { id }));

        return response;
    }
}

和我的路线定义(Global.asax中):

And my routes definition (Global.asax):

public static void RegisterRoutes(RouteCollection routes)
{
    // Default route
    routes.MapHttpRoute(
        name: "Default",
        routeTemplate: "{controller}/{id}",
        defaults: new { id =  RouteParameter.Optional }
    );

    // A route that enables RPC requests
    routes.MapHttpRoute(
        name: "RpcApi",
        routeTemplate: "rpc/{controller}/{action}",
        defaults: new { action = "Get" }
    );
}

到目前为止,如果我打的浏览器:

So far if I hit the browser with:


  • [baseaddress] /位置/ s0m3-gu1d-g0e5-hee5eeeee //它的工作原理

  • [baseaddress] /位置/ //多找到结果

  • [baseaddress] / RPC /位置/哑 //它的工作原理

  • [baseaddress]/locations/s0m3-gu1d-g0e5-hee5eeeee // It works
  • [baseaddress]/locations/ // Multiple results found
  • [baseaddress]/rpc/locations/dummy // It works

最奇怪的是,这个用来工作,直到我搞砸了我的NuGet在执行一些更新。缺少什么我在这里?

The strangest thing is that this used to work, until I messed up with my NuGet while performing some updates. What am I missing here?

开始GET动词,POST,PUT或删除将automapped的第一条路,我的假人的测试方法是通过RPC调用,这将落入第二条路线。

Verbs starting with GET, POST, PUT or delete would be automapped to the first route and my dummy test method would be called via rpc, which would fall into the second route.

时引发的误差出现InvalidOperationException 与消息

多重行动中发现符合要求,即:
  System.Linq.IQueryable`1 [Myproject.Domain.Location]获得()的类型Myproject.Webservices.Controllers.LocationsController
  在类型Myproject.Webservices.Controllers.LocationsController System.Net.Http.Htt presponseMessage假人()

Multiple actions were found that match the request: System.Linq.IQueryable`1[Myproject.Domain.Location] Get() on type Myproject.Webservices.Controllers.LocationsController System.Net.Http.HttpResponseMessage Dummy() on type Myproject.Webservices.Controllers.LocationsController

任何想法?

推荐答案

现在的问题是,该路由加载顺序。如果他们是这样的:

The problem is in the order that the routes are loaded. If they are like this:

// A route that enables RPC requests
routes.MapHttpRoute(
    name: "RpcApi",
    routeTemplate: "rpc/{controller}/{action}",
    defaults: new { action = "Get" }
);

// Default route
routes.MapHttpRoute(
    name: "Default",
    routeTemplate: "{controller}/{id}",
   defaults: new { id =  RouteParameter.Optional }
);

这将正常工作。首先请求将被映射对RPC,然后对控制器(thaty可能有或没有一个ID)。

It will work fine. First the request will be mapped against the RPC, then against the controller (thaty may have or not an Id).

这篇关于多个动作被发现匹配的要求,即的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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