gpt4 book ai didi

asp.net-mvc - ASP.NET Web API 错误 : No HTTP resource was found that matches the request URI

转载 作者:行者123 更新时间:2023-12-02 02:13:58 26 4
gpt4 key购买 nike

我正在尝试将 Web API 添加到现有的 ASP.NET MVC 项目中。这最初是一个 ASP.NET MVC 2 网站,后来使用以下过程升级到 MVC 3,然后再次升级到 MVC 4:

Upgrading an ASP.NET MVC 2 Project to ASP.NET MVC 3

Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4

我的 Api Controller 非常简单。实际上,它是默认模板:

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 路由。

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调用

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

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

但是,当我尝试访问“localhost:50315/api/values”时,出现以下错误:

<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>

根据路由调试器,路由似乎工作正常,Web api 路由是第一个“匹配当前请求”的条目。

最佳答案

请参阅以下步骤 - 它可能不会给您正确的答案,但会给您调试问题的方向。您可以一项一项地执行这些操作。

  1. 确保您已执行以下操作:

    Install-Package Microsoft.AspNet.WebApi

这只是为了确保您拥有正确的 DLL。

  • 您可以尝试的另一件事是在 get 顶部添加一条路由。

    class ValuesController : ApiController
    {
    [System.Web.Http.Route("api/values")]
    public IEnumerable<string> Get()
    {
    return new string[] { "value1", "value2" };
    }

    public string Get(int id)
    {
    return "value";
    }
    }
  • 禁用 MvC 路由进行调试。即

    protected void Application_Start()
    {
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    }
  • 启用 GET 动词。

  • 我在下面给你我的项目作为引用。

     <httpProtocol>
    <customHeaders>

    <add name="Access-Control-Allow-Headers" value="*" />
    <add name="Access-Control-Allow-Methods" value="*" />
    </customHeaders>
    </httpProtocol>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
    <remove name="WebDAV" />
    <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
    <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
    <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>

    关于asp.net-mvc - ASP.NET Web API 错误 : No HTTP resource was found that matches the request URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15667030/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com