简体   繁体   中英

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. 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. This creates an empty project with a single ValuesController . You should be able the JSON response by called /api/values , but this doesn't even work.

I'm using Fiddler to test the API locally and on the web server.

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.

Why in the world does this work locally but not on the production web server?

That the server returns 404 (Not Found) may indicate a lot of things. 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. 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.

After that,try to reach the Web ApI again.

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

Above comment of your's is suspicious, you should publish your WEB API application in the root directory. Like if http://example.com is pointing to "MyExample" folder, then application should be published on "MyExample" folder. After that you will be able access your api with http://example.com/api/ {controller}/{action}

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?

Also stick a plain old html file in the root of your project and see if the server serves it up.

in your case, since you create a subdomain of 'api', you should try

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

Please verify the .net framework on you hosted domain that may be old one.

  • Web api 2 is supposed on 4.5 framework.

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. In such a case you will receive message 404 or 500, and you would be lost why this routing is failing. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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