我知道这个网站和其他网站上已经被问过很多次了,但到目前为止我看到的解决方案都没有,所以我来了。
我在 mvc 4 项目中设置了一些 web api 路由。这是一个示例:
routes.MapHttpRoute(name: null, routeTemplate: "Client/Accounts/Save", defaults: new { controller = "AccountsApi", action = "SaveAccount" });
对应如下 Controller 和 Action :
public class AccountsApiController : ApiController
{
[HttpPost]
public object SaveAccount([FromBody] Account input)
{
.....
这在内置 VS Web 服务器中运行时有效,并且在 IIS7.5 的本地(Windows 7)安装上运行,没有做任何花哨的事情,但在 Server 2008 R2 上的 IIS7 中运行时会给出 404。
信息:
我试过的:
<modules runAllManagedModulesForAllRequests="true" />
设置(无论如何都是默认设置)该 api 仅使用
GET
和 POST
请求,这两个都不起作用。可能还值得一提的是,这些调用都是通过 ajax 进行的。system.web
, system.webServer
, runtime
配置中的部分:<system.web>
<compilation debug="false" targetFramework="4.0" />
<customErrors mode="Off" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<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" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
任何建议都非常感谢。谢谢。
更新
我重新实现了我的一个 Controller 以仅使用
HttpResponseMessage
和 HttpRequestMessage
[HttpGet]
public HttpResponseMessage GetAccount(HttpRequestMessage message)
{
message.CreateResponse(....
在本地运行(在 Win7 上安装 cassini 和完整的 IIS 本地安装)时,这再次按预期工作,但在推送到服务器时失败。
最佳答案
更新:我刚刚注意到你说 GET 方法也不起作用,在这种情况下,这个建议可能对你没有用。
尝试将方法的签名更改为
[HttpPost]
public HttpResponseMessage SaveAccount(HttpRequestMessage request)
{
}
这将允许您排除操作选择/模型绑定(bind)的问题。如果仍然不匹配,那么我建议您打开跟踪编写器并找出它无法找到 Controller 的原因。 http://nuget.org/packages/Microsoft.AspNet.WebApi.Tracing/
如果确实符合上述签名,请查看此博客文章 http://www.bizcoder.com/?p=259有关如何调试序列化问题的进一步建议。
关于asp.net-mvc - Web Api 调用在服务器上返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15809128/