ASP.NET 自托管网站 API 使用 IE – 无法显示此页面

ASP.NET Self hosted Web API with IE – This page cannot be displayed

我正在尝试在本地调用我的服务,但 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。

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

我刚试过,您的 api 在 Edge 和 Chrome 中工作正常。可能是 IE 没有发送正确的 Accept header 导致任一服务器 return 错误的结果或错误,或者 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=json and http://127.0.0.1:8080/api/web?type=xml 网址并检查自己。