简体   繁体   中英

C# Web API 2 app generates 404 on GET method when deployed to sub directory on IIS 7.5

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.

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.

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