2017-03-12 8 views
1

私のサービスをローカルで呼び出そうとしていますが、IEとEdgeはそれを見つけることができません。ASP.NET自己ホストされたWeb Api:IE - このページは表示されません

以下は、私が持っているコードスニペットであり、コンソールアプリケーションはエラーなしで動作しています。

Program.csの

public class Program 
    { 
     static void Main() 
     { 
      string baseAddress = "http://127.0.0.1:8080/"; 

      // Start OWIN host 
      using (WebApp.Start(url: baseAddress)) 
      { 
       Console.WriteLine("Service Listening at " + baseAddress); 

       System.Threading.Thread.Sleep(-1); 
      } 
     } 
    } 

Startup.cs

誰もがするように私はIE上で私のサービスを呼び出しています
public class Startup 
    { 
     public void Configuration(IAppBuilder appBuilder) 
     { 
      HttpConfiguration config = new HttpConfiguration(); 
      config.EnableCors(); 
      config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 

      appBuilder.UseWebApi(config); 
     } 
    } 

WebController.cs

public class Web 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
    } 

    [EnableCors(origins: "*", headers: "*", methods: "*")] 
    public class WebController : ApiController 
    { 
     Web[] websites = new Web[] 
     { 
      new Web { Id = 1, Name = "XYZ", Description = "XYZ"}, 
      new Web { Id = 2, Name = "ABC", Description = "ABC"} 
     }; 

     // GET api/Web 
     public IEnumerable Get() 
     { 
      return websites; 
     } 

     // GET api/Web/5 
     public Web Get(int id) 
     { 
      try 
      { 
       return websites[id]; 
      } 
      catch (Exception e) 
      { 
       return new Web(); 
      } 
     } 

     // POST api/values 
     public void Post([FromBody]string value) 
     { 
      Console.WriteLine("Post method called with value = " + value); 
     } 

     // PUT api/values/5 
     public void Put(int id, [FromBody]string value) 
     { 
      Console.WriteLine("Put method called with value = " + value); 
     } 

     // DELETE api/values/5 
     public void Delete(int id) 
     { 
      Console.WriteLine("Delete method called with id = " + id); 
     } 
    } 

http://127.0.0.1:8080/api/web Web全体をGETしますオブジェクト。

私はOWINとCORSという2つの追加パッケージをインストールしました。

は、誰かが私は試してみました。この問題

+0

IE/Edgeがdevtoolsコンソールにログするというエラーメッセージを含めるために質問を編集/更新できますか?リクエストを作成しているJavaScriptコードはどのように見えますか? – sideshowbarker

+0

クロム/郵便配達員を使用しようとします。私はエッジについてはわかりませんが、私は最近IEと同様の問題を抱えていました。なぜなら、localhostを解決する問題があるからです – MistyK

答えて

0

のための解決策を見つけるのに役立つ可能性があり、あなたのAPIは、エッジとChromeで正しく動作します。 IEが正しいAcceptヘッダーを送信しないと、サーバーが間違った結果やエラーを返すか、IEが応答を解釈できなくなる可能性があります。実際には、私はそれを正しく表示することはできませんので、チェックとIEのjsonファイルを保存するオファーを持っています。

public IHttpActionResult Get(string type = null) 
{ 
    var response = new HttpResponseMessage(HttpStatusCode.OK); 

    if (type == "json") 
    { 
     response.Content = new StringContent(JsonConvert.SerializeObject(websites)); 
     response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 
    } 
    else if (type == "xml") 
    { 
     response.Content = new StringContent("<xmlTag>Value</xmlTag>"); 
     response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/xml"); 
    } 

    return ResponseMessage(response); 
} 

http://127.0.0.1:8080/api/web?type=jsonhttp://127.0.0.1:8080/api/web?type=xmlのURLを追跡し、自分でチェックしてみてください。それは明示的に私はあなたが少しのコードを変更した表示するには

関連する問題