【问题标题】:MVC WebApi Routing No HTTP resource was foundMVC WebApi 路由未找到 HTTP 资源
【发布时间】:2019-11-26 02:09:59
【问题描述】:

这是我第一次使用 MVC,我一直在尝试让 webApi 路由正常工作。

我有一个简单的控制器,看起来像这样

namespace XRM.Controllers
{
    public class PipelinesWebApiController : ApiController
    {
        static List<XRM.Models.XRM_PipelineStages> stages = new List<Models.XRM_PipelineStages>
        {
            new Models.XRM_PipelineStages() { stageName = "test" },
            new Models.XRM_PipelineStages() { stageName = "test2" },
            new Models.XRM_PipelineStages() { stageName = "test3" }
        };

        public IEnumerable<XRM.Models.XRM_PipelineStages> Get()
        {
            return stages;
        }

        public XRM_PipelineStages Get(int id)
        {
            return stages.FirstOrDefault(s => s.ROWUID == id);
        }
    }
}

WebApiConfig:

namespace XRM {

    public static class WebApiConfig {
        public static void Register(HttpConfiguration config) {

            config.MapHttpAttributeRoutes();

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

}

Global.asax.cs

namespace XRM {

    public class MvcApplication : HttpApplication {
        protected void Application_Start() {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            DevExtremeBundleConfig.RegisterBundles(BundleTable.Bundles);
        }

    }
}

每当我尝试使用

http://localhost:58061/api/PipelinesWebApi

我收到错误消息:

未找到与请求 URI“http://localhost:58061/api/PipelinesWebApi”匹配的 HTTP 资源。

我检查了我的 WebApiConfig.cs 中也有 config.MapHttpAttributeRoutes();

我真的不知道为什么我似乎无法到达它。

【问题讨论】:

  • 我已经尝试过您的代码,但无法重现此问题。在我这边,它工作正常。
  • @ReyanChougle 你在使用 MVC5 吗?
  • 是的。是MVC5
  • @BloopieBloops 当前显示的内容应该可以工作。这可能是XY problem

标签: c# .net asp.net-mvc asp.net-web-api asp.net-web-api-routing


【解决方案1】:

您的代码在我的最后运行良好。我无法重现此问题。无论如何,您可以尝试另一种路由方式,如下所示:

WebApiConfig.cs 中的routeTemplate 更改为"api/{controller}/{action}/{id}",并调用URL 以及操作名称,例如api/PipelinesWebApi/Get

【讨论】:

  • 我建议创建一个新项目并重试。
  • 您好,我已添加 Global.asax
  • 我想我必须为我的任务使用另一种不需要 WebApi 的方法
  • 你找到解决办法了吗?
  • 我重新创建了项目
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
  • 2019-02-20
  • 2013-11-28
  • 2020-03-05
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
相关资源
最近更新 更多