且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

的ASP.NET Web API错误:没有HTTP资源发现,请求URI相匹配

更新时间:2021-07-12 17:20:18

解决的办法是给apicontroller类不同的命名空间,就像AardVark71在评论建议。

The solution was to give the apicontroller class a different namespace, like AardVark71's suggestion in the comments.

现有的MVC没有像WebApplication.Controllers命名空间和Web API使用相同的命名空间。当我改变了它,一切正常。

The existing MVC had a namespace like WebApplication.Controllers, and the web api was using that same namespace. When I changed it, everything worked.

完整的Web API控制器看起来是这样的:

The complete Web Api controller looks something like this:

namespace WebApplication.Api.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }
    }
}