6

I have a problem that's driving me nuts.

I have a basic web api written in C# / web API 2 on .net 4.5. As long as I deploy the site to the root directory of IIS 7.5, it works fine - as soon as I deploy it to a sub directory, I get 404 errors for any method requests.

my setup is this:

I have a single controller called ServicesController with the following methods:

    public IEnumerable<Service> GetAllServices()
    {
        return Services;
    }

    public IHttpActionResult GetService(int id)
    {
        var Service = Services.FirstOrDefault((p) => p.Id == id);
        if (Service == null)
        {
            return NotFound();
        }
        return Ok(Service);
    }

}

}

I have a routing template set like this:

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }

if I check the request in fiddler as it's coming through, on the root directory (root directory being localhost, or wwwroot) the request looks right:

api/services/1 (for example)

In the sub directory (called ServicesApp), the request prepends the directory name, which (I think?) may be the problem (or not), like this:

ServicesApp/api/services/1

Is there something I need to change in my visual studio project settings to modify the base call, or could it be something else? I'm a bit of a web api 2 novice, so I'm sure it's something small I'm missing.

Other things I've tried based on other posts I've read here:

-iis integrated mode is enabled in my web.config

-i did try adding runAllManagedModulesForAllRequests option in web.config as well

I can create a sub application in IIS and define the sub directory as the root, and it'll work fine, but that's not a viable option since this is going to be deployed to a remote / shared windows host that doesn't allow that level of functionality.

Any help greatly appreciated. Apologies in advance if I missed an obvious answer while searching.

FroggedUp
  • 63
  • 3

1 Answers1

2

Once you move your app to a subfolder, do you have a different app in the root then? web.config files inherit settings from parents apps, so if your parent is defining a setting and you place a child app which defines the same setting you might get an error (depends on which setting).

Also in IIS do you right click on the child app and select "Convert to Application"? Every application and "Child" application needs to be setup in IIS using either "Add an Application" or "Convert to Application".

Maybe you need to speak with the owner of the remote/shared windows host to find out how to do this through them. They might require another subscription for every app.

Also what path shows up when in IIS you select the webapi app folder and then click "Browse" it should open a browser with the right path in it to the root of the app.

  • Good thought on asking the remote host about different settings. In the root app. there aren't any other web.configs - also a good thought, but I verified it isn't an inheritance conflict by moving all the pre-existing files from the root folder somewhere else temporarily. – FroggedUp Nov 06 '14 at 22:35
  • So apparently you can't create virtual applications or additional virtual folders in hostgator's setup. they have the main IIS app, and that's it. best i can do is create subfolders, but not define them as virtual folders or sub applications. quite annoying. Is it even possible to run an MVC / webapi2 app from a subfolder that isn't a virtual application? – FroggedUp Nov 07 '14 at 08:18
  • My understanding is that you can't run apps from a subfolder that isn't setup as a virtual application. You could setup each app in different solutions and publish them to different hostgator accounts, but I imagine it'd make development a lot more annoying; and maybe there'd be issues with the webbrowser complaining about fetching data from a different host name. – Matt Lengenfelder Nov 07 '14 at 15:42
  • Maybe it is a WebApi issue - can you publish an asp.net mvc app in a subfolder and get that to work? If it works then WebApi should be workable, if you can't get that to work either then you'll probably need a different web host. I just ran across this: http://stackoverflow.com/questions/16812995/all-requests-to-asp-net-web-api-return-404-error which might be related, but I'd try to get a simple Hello World MVC app in a subfolder to work first. – Matt Lengenfelder Nov 07 '14 at 16:01