简体   繁体   中英

Post/Get works with VS Development server but not with IIS7

I have created a web page using Asp.Net MVC 4 (VS2010 SP1, Windows 7). It also has an api through what I can make a search or upload something (data and files). When I deploy the page on the development server of VS2010 SP1, all works fine but when I select IIS (IIS7), the api does not work any more. The "IsSuccessStatusCode" seems to be false. But the page itself works and can be acessed from other machines.

This is the address I use for the inbuilt dev server: "http://localhost:56272/api/" And this I use for IIS: "http://127.0.0.1/api/"

I have created a virtual directory for IIS (done by VS2010). Do I need to configure something in addition or could it be a routing problem that only exists for IIS?

This is my client side - class MyService - for the GET:

 public MyService(string serviceAdress) //"http://127.0.0.1/api/"
        {
            this.serviceAdress = new Uri(serviceAdress); //of type Uri
            client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
        }
    public Task<IEnumerable<Item>> SearchByTag(string tag)
    {
        client.BaseAddress = serviceAdress;
        var getStuffCall=client.GetAsync("Search/ByTag/" + tag);
        var r=getStuffCall.ContinueWith(
            t =>t.Result.IsSuccessStatusCode? (t.Result.Content.ReadAsAsync<IEnumerable<Item>>().Result):new List<Item>()
            );
        return r;
    }

My server side looks like this:

public class SearchController : ApiController
    {
        [HttpGet]
        public IEnumerable<Shared.Item> ByTag(string search)
        {....}
    }

And this is the routing in WebApiConfig:

config.Routes.MapHttpRoute(
            name: "SearchApi",
            routeTemplate: "api/Search/{action}/{search}",
            defaults: new
                          {
                              controller="Search"                                  
                          }
        );

You need to make sure that the user that web site and application pool have write permissions to the directory you're performing your upload to. In the development server, it doesn't come into play because that runs under your local (or domain) account which has normal privileges. IIS7 is running under a restricted user account, so its privileges must either be adjusted or the folder you're uploading to needs to include this user with write privileges.

Specific instructions for adjusting privileges can be found here: http://support.microsoft.com/kb/979124

The address for IIS was wrong. Instead of "http://127.0.0.1/api/" it had to be "http://127.0.0.1/MyVirtualFolderSomething/api/".

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