C# 带有IE的ASP.NET自托管Web API–;无法显示此页面

C# 带有IE的ASP.NET自托管Web API–;无法显示此页面,c#,asp.net,owin,C#,Asp.net,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: baseAddres

我试图在本地调用我的服务,但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上调用我的服务,就像每个人一样:获取整个Web对象

我已经安装了另外两个软件包,OWIN和CORS


有人能帮我找到解决这个问题的方法吗。

我刚刚试过,你的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);
}
公共IHttpActionResult获取(字符串类型=null) { var响应=新的HttpResponseMessage(HttpStatusCode.OK); 如果(类型==“json”) { response.Content=newStringContent(JsonConvert.SerializeObject(网站)); response.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/json”); } else if(type==“xml”) { 响应。内容=新内容(“值”); response.Content.Headers.ContentType=新的MediaTypeHeaderValue(“text/xml”); } 返回响应消息(response); }
尝试跟踪URL和URL并检查您自己。

我刚刚尝试过,您的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);
}
公共IHttpActionResult获取(字符串类型=null) { var响应=新的HttpResponseMessage(HttpStatusCode.OK); 如果(类型==“json”) { response.Content=newStringContent(JsonConvert.SerializeObject(网站)); response.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/json”); } else if(type==“xml”) { 响应。内容=新内容(“值”); response.Content.Headers.ContentType=新的MediaTypeHeaderValue(“text/xml”); } 返回响应消息(response); }
尝试跟踪URL和URL并检查您自己。

您是否可以编辑/更新您的问题,以包括IE/Edge记录到devtools控制台的任何错误消息?你的JavaScript代码是什么让请求看起来像这样的呢?试着使用chrome/postman。我不知道edge,但我最近在IE上遇到了类似的问题,因为它在解决LocalHost方面存在问题。您可以编辑/更新您的问题,以包括IE/edge记录到devtools控制台的任何错误消息吗?你的JavaScript代码是什么让请求看起来像这样的呢?试着使用chrome/postman。我不知道edge,但我最近在IE上也遇到了类似的问题,因为它在解决localhost方面存在问题