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

问题描述 投票:1回答:1

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

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

c# asp.net owin
1个回答
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网址并检查一下自己。

© www.soinside.com 2019 - 2024. All rights reserved.