【问题标题】:No HTTP resource was found that matches the request URI未找到与请求 URI 匹配的 HTTP 资源
【发布时间】:2017-06-28 14:01:18
【问题描述】:

我检查了一些链接以了解错误原因,但没有解决我的问题。

我正在尝试从 Postman 访问 WebAPI 操作,但收到以下错误。

"message": "未找到与请求 URI 'http://localhost:50684/api/albums/history' 匹配的 HTTP 资源。",
"messageDetail": "没有找到与名为 'album' 的控制器匹配的类型。"

我的 API。

[Authorize]
public class AlbumsController : ApiController
{

   [HttpGet]
   [Route("api/album/history/")]
   public BaseTO GetHistory(string type,int id)
   {  
      //api stuff
   }
 }

我试过api/album/history/{anything}

当邮递员打电话时,我路过:-

  • 授权令牌
  • 类型
  • 身份证

非常感谢任何帮助/建议。 谢谢

【问题讨论】:

  • 你是否在 api config 中启用了属性路由? config.MapHttpAttributeRoutes();
  • URL 和错误信息不匹配。它显示 URL 正在寻找 AlbumController 但该 URL 有专辑(复数)
  • 您可以尝试使用localhost:50684/api/albums/history?type=a&id=1,并且您是否在向邮递员发出请求时在标头中添加授权令牌?

标签: asp.net-web-api postman


【解决方案1】:

您是否在 api 配置中启用了属性路由? config.MapHttpAttributeRoutes();

使用您当前的路线,您应该通过查询字符串http://localhost:50684/api/albums/history?type=test&id=1 访问它,并使用[FromUri] 装饰参数

[HttpGet]
   [Route("api/albums/history/")]
   public IHttpActionResult GetHistory([FromUri]string type,[FromUri]int id)
   {  
      //api stuff
   }

或通过路由参数访问 api - http://localhost:50684/api/albums/history/test/1

[HttpGet]
   [Route("api/albums/history/{type}/{id}")]
   public IHttpActionResult GetHistory(string type,int id)
   {  
      //api stuff
   }

【讨论】:

    【解决方案2】:

    你的代码应该是这样的:

    [Authorize]
    public class AlbumsController : ApiController
    {
    
       [HttpGet]
       [Route("api/albums/history/")]
       public IHttpActionResult GetHistory(string type,int id)
       {  
          //api stuff
       }
     }
    

    您从 Postman 打来的电话应该是这样的,并确保您在 Postman 中的方法是 GET:

    http://localhost:50684/api/album/history?type=test&id=1

    希望对你有所帮助。

    【讨论】:

    • 您更新了返回类型?有什么区别??
    • IHttpActionResult 会派上用场,当您想预设 HTTP 响应的特定标头/属性时。例如,在响应 POST 请求时,您希望自动将状态码设置为 201(已创建)并设置位置标头。
    • 嘿@Kgn-web 你能详细说明我的代码有什么问题吗?
    • 请注意,定义的路由包含字符串“albums”,但在用于调用端点的 url 中,其书面“album”
    【解决方案3】:

    我有同样的问题,我只是改变了这个:

    AllowInsecureHttp = true

        public void ConfigureAuth(IAppBuilder app)
        {
    
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
    
    
            app.UseCookieAuthentication(new CookieAuthenticationOptions());
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    
    
            PublicClientId = "self";
            OAuthOptions = new OAuthAuthorizationServerOptions
            {
                TokenEndpointPath = new PathString("/api/Token"),
                Provider = new ApplicationOAuthProvider(PublicClientId),                
                AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
                AllowInsecureHttp = true
            };
    
            app.UseOAuthBearerTokens(OAuthOptions);
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 2016-12-01
      • 2016-01-26
      • 2017-06-17
      • 1970-01-01
      相关资源
      最近更新 更多