简体   繁体   English

ASP.NET WEB API 2本地工作但不在生产Web服务器上

[英]ASP.NET WEB API 2 Works Locally But Not On Production Web Server

I've developed a new WEB API 2 that works great locally, however when I upload the same code to my production server (Arvixe in this case) all I get is a 404 when I call it. 我已经开发了一个在本地运行良好的新WEB API 2,但是当我将相同的代码上传到我的生产服务器(在这种情况下是Arvixe)时,我得到的就是404。 I've spent HOURS searching the web, reading forums, etc.. and have been able to find no resolution, so I'm asking here as my last effort. 我花了两个小时在网上搜索,阅读论坛等等。并且找不到解决方案,所以我在这里问我最后的努力。

I'm currently only testing with the default project that gets created when you do New Project > ASP.NET Web API 2 Empty Project in Visual Studio. 我目前只测试在Visual Studio中执行New Project> ASP.NET Web API 2空项目时创建的默认项目。 This creates an empty project with a single ValuesController . 这将创建一个带有单个ValuesController的空项目。 You should be able the JSON response by called /api/values , but this doesn't even work. 您应该通过调用/api/values来进行JSON响应,但这甚至不起作用。

I'm using Fiddler to test the API locally and on the web server. 我正在使用Fiddler在本地和Web服务器上测试API。

http://localhost:1993/api/values    <--- works great

but

http://api.mydomain.com/api/values   <--- returns 404

Note: I created a subdomain "api" in this case, but everything for the code for the API is unchanged from when it was created. 注意:在这种情况下我创建了一个子域“api”,但是API的代码的所有内容都与创建时没有变化。

Why in the world does this work locally but not on the production web server? 为什么世界上这在本地工作,而不是在生产Web服务器上?

That the server returns 404 (Not Found) may indicate a lot of things. 服务器返回404(未找到)可能表示很多事情。 However you can check using the following step: 但是,您可以使用以下步骤进行检查:

  1. Add a simple text document like readme.txt to your a folder sub-domain http://api.mydomain.com , and try to get access to that. 将一个简单的文本文档(如readme.txt)添加到您的文件夹子域http://api.mydomain.com ,并尝试访问它。 If you can't access to that file, it means that the subdomain is not configured properly. 如果您无法访问该文件,则表示未正确配置子域。

  2. Publish the webservice using the "Publish" functionality, so that all DLLs will be copied. 使用“发布”功能发布Web服务,以便复制所有DLL。

After that,try to reach the Web ApI again. 之后,尝试再次访问Web ApI。

Hope that help. 希望有所帮助。

"Note: I created a subdomain 'api' in this case, but everything for the code for the API is unchanged from when it was created." “注意:在这种情况下我创建了一个子域'api',但是API的代码的所有内容都与创建它时没有变化。”

Above comment of your's is suspicious, you should publish your WEB API application in the root directory. 您的上述评论是可疑的,您应该在根目录中发布您的WEB API应用程序。 Like if http://example.com is pointing to "MyExample" folder, then application should be published on "MyExample" folder. 就像http://example.com指向“MyExample”文件夹一样,应用程序应该发布在“MyExample”文件夹中。 After that you will be able access your api with http://example.com/api/ {controller}/{action} 之后,您将可以通过http://example.com/api/ {controller} / {action}访问您的API

Just a simple suggestion which I'm sure you have already considered, but have you opened the http port 80 on the server's firewall? 只是一个简单的建议,我相信你已经考虑过,但是你在服务器的防火墙上打开了http端口80吗?

Also stick a plain old html file in the root of your project and see if the server serves it up. 还要在项目的根目录中粘贴一个普通的旧html文件,看看服务器是否提供了它。

in your case, since you create a subdomain of 'api', you should try 在您的情况下,因为您创建了'api'的子域,您应该尝试

http://api.mydomain.com/api/api/values

note that if you're using database for the function, you should change the connectionString in your web config 请注意,如果您正在为该函数使用数据库,则应更改Web配置中的connectionString

Please verify the .net framework on you hosted domain that may be old one. 请验证您托管域上可能是旧域的.net框架。

  • Web api 2 is supposed on 4.5 framework. Web api 2应该是4.5框架。

One reason for web api 2 method working OK on local machine but not on production server is that the method you are calling is working on local machine but not on remote server. Web api 2方法在本地计算机上正常工作但在生产服务器上工作正常的一个原因是您调用的方法是在本地计算机上工作,而不是在远程服务器上工作。 In such a case you will receive message 404 or 500, and you would be lost why this routing is failing. 在这种情况下,您将收到消息404或500,并且您将丢失为什么此路由失败。 Why a method would fail on remote server, well there may be many reasons. 为什么一个方法会在远程服务器上失败,原因可能有很多。 For me, I was querying database in my method and my connectionString was not set for remote server. 对我来说,我在我的方法中查询数据库,而我的connectionString没有为远程服务器设置。 One way of resolving it would be to put some very simple code in that particular method and test that routing is working. 解决它的一种方法是在该特定方法中放入一些非常简单的代码并测试路由是否正常工作。 Then check your original code for errors reasons. 然后检查原始代码是否有错误原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM