ASP.net WEB API CORS 404错误 [英] ASP.net WEB API CORS 404 error

查看:185
本文介绍了ASP.net WEB API CORS 404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用集成的ASP.net主机和WEB API创建Windows服务。
主机和API可以正常工作。当我使用Internet浏览器时,我从控制器收到响应
,因此没有权限问题。

I create a Windows Service with a integrated ASP.net Host and WEB API. The host and API works fine. When I use my internet Browser I get response from my controller, so no permission problems.

WebService配置:

WebService configuration:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddCors();
    services.Configure<MvcOptions>(options =>
    {
        options.Filters.Add(new CorsAuthorizationFilterFactory("AllowAll"));
    });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });

    app.UseCors(builder =>
    {
        builder.AllowAnyOrigin()
        .AllowAnyHeader()
        .AllowAnyMethod();
    });
}

控制器:

[Route("")]
public class TimerController : Controller
{
public static List<Timer> timers = new List<Timer> { };

// url:localhost:48523
public IEnumerable<Timer> GetAllTimers()
{
    return timers;
}
}

现在我尝试使用CORS来获取信息WEB API。
对于CORS请求,我使用了JavaScript。

Now I try to use CORS to get the information from the WEB API. For the CORS request I used JavaScript.

var json;
var url = "http://192.168.87.58:48523";

function sendRequest()
{
    json = new XMLHttpRequest();
    json.open("GET", url, true);
    json.send();
    console.log(json.responseText);
}

但是,如果我尝试获取响应,则会收到错误消息:404 File not found并带有以下注释:

But if I try to get response I get the error: 404 File not Found with follow note:


XML解析错误:未找到根元素位置:
http://192.168.87.58:48523/ 行号1,第1列:

如果我打开此页面,则会看到原始数据视图:

If I open up this page I get the raw data view:


[{ computerName: XX -XX-XX-XX-XX,计算机IP: 192.168.XX.XX, lastLogin: 2017-XX-29T16:XX:XX.438988 + 02:00,计划关闭时间: 2017 -XX-02T09:XX:XX.438988 + 02:00, isRunning:true, hoursToSleep:48.0}]

[{"computerName":"XX-XX-XX-XX-XX","computerIP":"192.168.XX.XX","lastLogin":"2017-XX-29T16:XX:XX.438988+02:00","plannedShutdownTime":"2017-XX-02T09:XX:XX.438988+02:00","isRunning":true,"hoursToSleep":48.0}]

如果我看一下标题信息,一切都很好。
标题信息。
我需要帮助,我可以找不到任何错误。

If I take a look in the header information, it is everything fine. Header information. I need help, I can't find any bug.

推荐答案

我认为您的cors配置不正确。 Web客户端和Web api连接不同,所以我理解您的问题。

I think your cors configuration is not correct. Web client and web api connections are different so I understand your problem.

我在下面编写我们的配置,以便您可以检查它。

I'm writing our configuration below so you can check it.

Install-Package Microsoft.Owin.Cors -Version 3.1.0

Startup.cs:

Startup.cs:

app.UseCors(CorsOptions.AllowAll)

网络配置:

<system.webServer>
  <handlers>
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <remove name="OPTIONSVerbHandler" />
    <remove name="TRACEVerbHandler" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
</system.webServer>



ASP.NET(System.Web)



ASP.NET (System.Web)

Install-Package Microsoft.AspNet.WebApi.Cors

App_Start / WebApiConfig-注册方法:

App_Start/WebApiConfig - Register method:

config.EnableCors();

FooController:

FooController:

[EnableCors(origins: "*", headers: "*", methods: "*")]

这篇关于ASP.net WEB API CORS 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆