的ASP.NET Web API错误:没有HTTP资源发现,请求URI相匹配 [英] ASP.NET Web API Error: No HTTP resource was found that matches the request URI

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

问题描述

我试图在Web API添加到现有的ASP.NET MVC项目。这本来是一个ASP.NET MVC 2的Web站点使用下列程序,后来升级到MVC 3,然后再MVC 4:

I am trying to add the Web API to an existing ASP.NET MVC project. This was originally a ASP.NET MVC 2 web site that was later upgraded to MVC 3 and then again to MVC 4 using the following procedures:

升级一个ASP.NET MVC 2项目到ASP.NET MVC 3

升级的ASP.NET MVC 3项目到ASP.NET MVC 4

我的API控制器是pretty简单。其实,这是默认的模板:

My Api controller is pretty straightforward. Actually, it is the default template:

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

我添加了一个类注册日API路线。

I've added a class to register de API routes.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

这是从所谓的Application_Start

That is called from Application_Start

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

然而,当我尝试访问本地主机:50315 / API /值',我得到以下错误:

However, when I try to access 'localhost:50315/api/values', I get the following error:

<Error>
<Message>
    No HTTP resource was found that matches the request URI    'http://localhost:50315/api/values'.
</Message>
<MessageDetail>
    No type was found that matches the controller named 'values'.
</MessageDetail>
</Error>

根据路由调试,路线似乎是正常工作,网络API路线是第一个条目'匹配当前请求。

According to the Routing Debugger, the route seems to be working properly, the web api route is the first entry that 'Matches Current Request'.

推荐答案

解决的办法是给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";
        }
    }
}

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

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