没有HTTP资源被发现在的ASP.NET Web API请求URI匹配错误 [英] No HTTP resource was found that matches the request URI error in ASP.NET Web API

查看:3324
本文介绍了没有HTTP资源被发现在的ASP.NET Web API请求URI匹配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我TransferController类的草图。

这一切的Web API code。

All this is Web API code.

public class TransferController : ApiController
{
  [HttpGet, ActionName("Queue")]
  public IEnumerable<object> GetQueue(Guid sessionId) {...}

  [HttpDelete, ActionName("Delete")]
  public void Delete(Guid sessionId, Guid fileId) {...}

  [HttpGet, ActionName("Cancel")]
  public bool Cancel(Guid sessionId, Guid fileId) {...}

  [HttpGet, ActionName("UploadedBytes")]
  public long GetUploadedByteCount(Guid sessionId, Guid fileId) {...}

  [HttpGet, ActionName("DownloadUrl")]
  public string GetDownloadUrl(string fileId) {...}

  [HttpPost, ActionName("FileChunk")] 
  public void PostFileChunk([FromUri]Guid sessionId, [FromUri]Guid fileId) {...}

  [HttpPost, ActionName("UploadDefinition")]
  public Guid PostUploadItem([FromBody]UploadDefinition uploadDef) {...}

}

这是路由。

public static void Register(HttpConfiguration config)
{
  config.Routes.MapHttpRoute(
    name: "DefaultApi", 
    routeTemplate: "api/{controller}/{action}"
    );
  config.Routes.MapHttpRoute(
    name: "DefaultApiDefaultMethod", 
    routeTemplate: "api/{controller}"
    );
}

这是调用。

$.ajax({
  url: "api/Transfer/Queue",
  data: { sessiondId: login.SessionId() }    
})
.done(function (result) {
  history.push(new UploadItem());
  for (var i = 0; i < result.length; i++) {
    var ui = new UploadItem(result[i]);
    history.push(ui);
  }
})
.fail(function (result) {
  app.showMessage(JSON.parse(result.responseText).Message);
});

这是结果。

无HTTP资源发现,请求URI相匹配'http://localhost:54770/api/Transfer/Queue?sessiondId=0e2c47b9-e674-446d-a06c-ce16932f9580'.

这是我的UserController类的草图。

public class UserController : ApiController 

  [HttpGet, ActionName("Authenticate")]
  public object Authenticate(string email, string password) {...}

  [HttpPost]
  public void Register([FromBody]UserDefinition userDef) {...}

  [HttpGet, ActionName("Pulse")]
  public bool Pulse(Guid sessionId) {...}

}

有关原因深不可测给我,我没有问题要求在UserController的东西。参数被编组以完全相同的方式,和所述的相同的航线中使用。

For reasons unfathomable to me, I have no trouble calling anything in the UserController. Parameters are marshalled in exactly the same way, and the same routes are in use.

下面达雷尔 - 米勒使用单元测试,以验证路线。坦率地说,我踢自己没有这种思维,而现在我已经做了相同的。

Darrel Miller below uses unit testing to validate routes. Frankly I'm kicking myself for not thinking of this, and now I've done the same.

不过测试,他显示他们真正测试URL的唯一解析。例如,该测试通过

But tests as he shows them really test only parsing of the URL. For example, this test passes

public void TestMvc4RouteWibble()
{
  var config = new HttpConfiguration();
  config.Routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: "api/{controller}/{action}/{id}",
      defaults: new { id = RouteParameter.Optional }
      );


  var route =
      config.Routes.GetRouteData(new HttpRequestMessage()
      {
        RequestUri = new Uri("http://localhost:54770/api/Transfer/Wibble?sessionId=0e2c47b9-e674-446d-a06c-ce16932f9580&fileId=0e2c47b9-e674-446d-a06c-ce16932f9581")  //?
      });

  Assert.IsNotNull(route);
  Assert.AreEqual("Transfer", route.Values["controller"]);
  Assert.AreEqual("Wibble", route.Values["action"]);

}

尽管明显缺乏一个方法的传输控制器上维布勒。

despite the conspicuous absence of a method Wibble on the Transfer controller.

另外路由对象实际上不是一个HttpRoute对象,这是一个HttpRouteData对象。但是这平凡的修正。该HttpRoute对象可作为HttpRouteData对象的属性

Also the route object is not actually a HttpRoute object, it's a HttpRouteData object. But that's trivially corrected. The HttpRoute object is available as a property of the HttpRouteData object.

public void TestMvc4RouteWibble()
{
  var config = new HttpConfiguration();
  config.Routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: "api/{controller}/{action}/{id}",
      defaults: new { id = RouteParameter.Optional }
      );


  var routeData =
      config.Routes.GetRouteData(new HttpRequestMessage()
      {
        RequestUri = new Uri("http://localhost:54770/api/Transfer/Wibble?sessionId=0e2c47b9-e674-446d-a06c-ce16932f9580&fileId=0e2c47b9-e674-446d-a06c-ce16932f9581")  //?
      });

  Assert.IsNotNull(routeData);
  Assert.AreEqual("Transfer", routeData.Values["controller"]);
  Assert.AreEqual("Wibble", routeData.Values["action"]);

}

和它又将有一个处理程序属性。然而,这是比它可能是不够丰富,因为一个空的处理程序仅仅意味着(从MSDN)

And it in turn has a Handler property. However this is less informative than it might be, since a null handler simply means (from MSDN)

如果为空,默认的处理信息分配给IHttpController的实现。

If null, the default handler dispatches messages to implementations of IHttpController.

现在,我的控制器是由ApiController这无疑实现了ExecuteAsync方法是由IHttpController接口指定的唯一来源。这是我想象的手段,如果我知道更多关于它,我可以测试方法的执行。

Now, my controller is derived from ApiController which certainly implements the ExecuteAsync method that is the only thing specified by the IHttpController interface. Which I imagine means I could test execution of that method if I knew more about it.

推荐答案

OK,然后...感谢把单元测试想法我的头,它加快东西极大。

OK then... thanks for putting the unit test idea in my head, it sped things up immensely.

这里的内幕:

您可以有不同的动词相同的参数签名(获得放后删除)。

You can have identical parameter signatures on different verbs (get post put delete).

您的不能有不同的操作名称相同的参数签名在同一个动词。

You cannot have identical parameter signatures on different action names on the same verb.

您只需要有所不同的有一个参数名称。

You only need vary one parameter name.

因此​​,这是确定的,因为他们都在不同的动词

So this is ok because they're all on different verbs

[HttpDelete, ActionName("Delete")]
public void Delete(Guid sessionId, Guid fileId) {...}

[HttpGet, ActionName("Cancel")]
public bool Cancel(Guid sessionId, Guid fileId) {...}

[HttpPost, ActionName("FileChunk")] 
public void PostFileChunk(Guid sessionId, Guid fileId) {...}

但由于他们都得到这个不冷静

but this is not cool because they're both gets

[HttpGet, ActionName("UploadedBytes")]
public long GetUploadedByteCount(Guid sessionId, Guid fileId) {...}

[HttpGet, ActionName("Cancel")]
public bool Cancel(Guid sessionId, Guid fileId) {...}

和您可以修复它​​像这样

and you can fix it like this

[HttpGet, ActionName("UploadedBytes")]
public long GetUploadedByteCount(Guid sessionId, Guid uploadBytesFileId) {...}

[HttpGet, ActionName("Cancel")]
public bool Cancel(Guid sessionId, Guid cancelFileId) {...}

也许我是一个硬的屁股,但据我而言它不是路由,直到方法被调用。

Maybe I'm a hard-ass but as far as I'm concerned it isn't routing until the method is called.

这篇关于没有HTTP资源被发现在的ASP.NET Web API请求URI匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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