21

I'm working on a ASP.NET MVC 4 website. Together with MVC controllers, I have one API controller which contains an ajax GET API interface. It works perfectly when debugged locally with visual studio.

However, after I deployed it as an Azure Website, MVC controllers work, but the API controller doesn't work anymore. When jQuery code tries to reach it, it returns a response like:

No HTTP resource was found that matches the request URI 'http://example.com'.

It looks like at least the route works (otherwise, another 404 response body is returned.).

Additionally, I have another pure MVC 4 Web API service deployed as an Azure Cloud Service. It works perfectly. Thus, I wonder what causes the API inside MVC Website to fail? Thank you!

abatishchev
  • 92,232
  • 78
  • 284
  • 421
Felix Yao
  • 231
  • 2
  • 4
  • Let's see if we can get more info by configuring Web API to display more error details. Just for quick debugging purposes, can you add the following to your global.asax.cs file: `GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;` Then use Fiddler to watch the request and response coming back. In the response, we'll now hopefully have some additional information to help us debug. – Mark Berryman Nov 16 '12 at 17:56
  • Thanks Mark for the hint. This time the detailed message is: No type was found that matches the controller named 'xxxx'. – Felix Yao Nov 16 '12 at 22:28
  • Okay, then run through the checks Youssef recommends. Although I'm surprised you don't have the same issue when debugging locally. – Mark Berryman Nov 17 '12 at 06:05
  • Is there any chance that MVC is not properly deployed to Azure? – sharptooth Jan 24 '13 at 08:13
  • @sharptooth Thanks for asking, it's been quite a long while actually... Finally I implement the desired feature with an mvc controller rendering partial view, instead of the previous api controller. I think Azure probably has some special server configuration for it, because it separates website and cloud service as two different types of instance. And further, when I deploy the work elsewhere, it works just fine. As I'm not a server management master, I won't go deep into it :) – Felix Yao Jan 24 '13 at 11:30
  • 1
    I have exactly the same issue with Web Api app working fine on local and cloud services web role machiens but not in AWS.. plus no detailed error messages can be retrieved other than "No type was found that matches the controller named 'xxx'" – Grief Coder Jun 18 '13 at 11:06
  • @GriefCoder did you mean Aamazon or Azure web service? If it is Amazon, I'm not sure. For Azure, I think the problem is with the IIS configuration. Azure has different instances for different types of communication. Respectively, different configurations are applied. E.g. in my case, I created one website instance. I think it has some constraints limited by config that cause the disability of API controller. Azure doesn't provide such flexibility to change the configuration to further extent. Anyway, you can create another cloud service instance dedicated for API only. That works for sure. – Felix Yao Jun 18 '13 at 17:24
  • Did you ever figure this out?! It's driving me nuts. – Ray Suelzer Oct 12 '13 at 18:39

4 Answers4

2

It is a common problem When an MVC application is deployed on IIS, the HTTP url which is calling the REST Service exposed by your web API will change. If your api controller is named MyApiController, so in production evironment, you should add the application name before the calling URL. That means that /api/MyApi will become /MyApp/api/MyApi. MyApp is the prefix of your web site (yourwebsite/MyApp).

Hamdi Baligh
  • 826
  • 1
  • 11
  • 29
2

We give a name to the controller via attribute based routing and a name to each of the endpoints inside the controller. Generic by convention GET, PUT, POST, PATCH are not allowed for security reasons.

Cybersecurity and our DevOps requires it for both firewall logs, performance monitoring and security monitoring.

Consider using:

    [Route("api/inventory")]
    public class InventoryController : ApiController
    {
      [HttpGet("inventorylist")]
      public IActionResult InventoryList() 
      { 
      }

      [HttpGet("inventoryitem")]
      public ActionResult InventoryItem([FromQuery] string itemId) 
      {
      }
    }
JohnT
  • 71
  • 4
1

Check for these things they usually help:

  1. Make sure your controller type is public and derives from ApiController
  2. Make sure the controller is named 'xxxxController' where xxxx is the controller name

In that case, the URL api/xxxx should work.

Youssef Moussaoui
  • 11,559
  • 2
  • 36
  • 35
  • 1
    Thank you for providing the checks. However, as I mentioned, they are all correctly derived or named, and it works fine in local debugging. – Felix Yao Nov 17 '12 at 07:58
1

I solved the issue. I use Telerik DataAccess as my ORM, and the project should have Copy Local to True.

Likewise, add all references as a Copy Local=True if they seem to be 3rd party dlls or from nuget.

You can find error more easily by adding <customErrors mode="Off" /> inside of <system.web> in Web.config file.

Youngjae
  • 21,562
  • 14
  • 100
  • 182