3

ルーティングとコントローラーに関する多くの質問を読みましたが、探しているものが見つかりません。私はこの構造を持つこのコントローラーを持っています:

更新:フルクラスのソースが含まれています。

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

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" }
    );
}

これまでのところ、次のようにブラウザをヒットした場合:

  • [baseaddress]/locations/s0m3-gu1d-g0e5-hee5eeeee// できます
  • [baseaddress]/locations/ //複数の結果が見つかりました
  • [baseaddress]/rpc/locations/dummy // できます

最も奇妙なことは、更新を実行しているときにNuGetを台無しにするまで、これが機能していたことです。ここで何が欠けていますか?

GET、POST、PUT、またはdeleteで始まる動詞は、最初のルートに自動マップされ、ダミーのテストメソッドがrpcを介して呼び出され、2番目のルートに分類されます。

スローされるエラーはInvalidOperationExceptionメッセージ付きです

リクエストに一致する複数のアクションが見つかりました: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

何か案は?

4

2 に答える 2

5

問題は、ルートがロードされる順序にあります。彼らがこのような場合:

// 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 に対してマップされ、次にコントローラーに対してマップされます (ID がある場合とない場合があります)。

于 2013-02-27T15:29:43.363 に答える