WP7 Device + Windows 8.1 + VS 2012 + Asp.net Web Api - How to make the device talk to Web Api?

I am now on Windows 8.1 and using VS 2012. I use the backend for web applications for all the hard work in my Windows Phone application. However, when I ran the application through the emulator, all requests for a locally executed web api were rejected.

I found that the emulator was unable to resolve localhost. Having done something very simulator in this article, I could not solve the problem with the Windows 8 emulator.

However, I need to test it on my device itself in order to use the camera, but I ran into the same problem as the emulator. I cannot communicate with my local web airline.

I had no problems when I was in Windows 7 and VS 2010. I just used the steps in this article and had no problems.

I get this error when I try to execute a request for my web api from my device (the device is connected via usb via computer)

<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\"http://www.w3.org/TR/html4/strict.dtd\">
<HTML>
  <HEAD>
    <TITLE>Bad Request</TITLE>
    <META HTTP-EQUIV=\"Content-Type\" Content=\"text/html; charset=us-ascii\">
  </HEAD>
  <BODY>
    <h2>Bad Request - Invalid Hostname</h2>
    <hr>
    <p>HTTP Error 400. The request hostname is invalid.</p>
  </BODY>
</HTML>

What do I need to configure in order to make this work?

Edit

I still have this problem. I was able to get the Windows Phone 8 emulator to work, but still can't get my WP7 device to be able to talk to the local web api.

Code sample I will do something like this

public class CourseService
{
    public const string webApiUrl = "http://localhost:4372/api/values";
    RestClient client = new RestClient(webApiUrl);
    public void GetCourses(Action successCallback, Action<string> errorCallBack, bool forceError = false)
    {
        var request = new RestRequest(Method.GET);

        client.ExecuteAsync(request, response =>
        {
            if (forceError)
            {
                errorCallBack("Failed");
            }
            else
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    successCallback();
                }
                else
                {
                    errorCallBack("Failed");
                }
            }
        });
    }
}



 public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
    }
  • I tried localhost (knew that this would not work with what I read)
  • tried the IP address of my computer.
  • tried to set ip address application in IIS Express to use

    < >           < binding protocol = "http" bindingInformation = ": 55210: localhost" /" >           < binding protocol = "http" bindingInformation = ": 55210: 169.254.80.80 "/" >       </ >

+4
5

, localhost. , IP- .

IP-, Fiddler WP7, , . Windows.

+1

webapi? IIS Express?

IIS Express VS2010

IIS? CORS. webapi 1 2?

, nuget: https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Cors/

CORS ( ) Webapi2 : http://msdn.microsoft.com/en-us/magazine/dn532203.aspx

, , webapi webapi dataannotations

0

, C:\Windows\System32\drivers\etc\hosts, LAN IE Internet Properties.

" " .

0

<bindings>
    <binding protocol="http" bindingInformation="*:55210:localhost" />
    <binding protocol="http" bindingInformation="169.254.80.80:55210:" />
</bindings>

<bindings>
    <binding protocol="http" bindingInformation=":55210:localhost" />
    <binding protocol="http" bindingInformation=":55210:169.254.80.80" />
</bindings>

, admin... iis express , .

0

All Articles