ASP.NET自托管Web Api:IE - 无法显示此页面

时间:2017-03-12 08:30:12

标签: c# asp.net cors owin

我试图在本地调用我的服务,但IE和Edge无法找到它。

以下是我的代码段,我的控制台应用程序正在运行,没有任何错误。

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

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);
        }
    }

我在IE上调用我的服务,就像所有人一样:http://127.0.0.1:8080/api/web来获取整个Web对象。

我已经安装了两个额外的软件包:OWIN和CORS。

有人可以帮助找到解决此问题的方法

1 个答案:

答案 0 :(得分:0)

我刚试过,你的api在Edge和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网址,然后自行检查。