简体   繁体   English

在Windows窗体中配置webapi以接受CROS

[英]Configure webapi in Windows Forms to accept CROS

I'm doing a project for college where one WebSite sends commands to a Windows Forms application. 我正在为一个大学项目,其中一个WebSite将命令发送到Windows Forms应用程序。 This application is Responsible for access to serial port and send and receive commands. 此应用程序负责访问串行端口以及发送和接收命令。

Communication between the website and the Windows Forms application, I used Web Api. 网站和Windows Forms应用程序之间的通信是使用Web Api。 I need to setup webapi Implementing CORS, but all examples I've seen are for MVC and I need for Windows Forms. 我需要设置webapi实施CORS,但是我看到的所有示例都是针对MVC的,而我则需要Windows Forms。 I placed the order that I'm calling from the client and how webapi in my Windows Forms is configured. 我下了从客户端调用的顺序,以及如何配置Windows窗体中的webapi。

Also, put the error that Chrome returns me. 另外,输入Chrome返回我的错误。

Class WebApi with Windows Forms Windows窗体类的WebApi

public class GPSController : ApiController
{
    [HttpGet]
    public Coordenada Posicao()
    {
        return TCCWindows.FormPrincipal.PegarCoordenadas();
    }
}

Configuration WebApi Windows Forms 配置WebApi Windows表单

private void button2_Click_2(object sender, EventArgs e)
{
    if (button2.Text == "Iniciar serviço")
    {
        var config = new HttpSelfHostConfiguration(textBox1.Text);

        config.Routes.MapHttpRoute(
            "API Default", "api/{controller}/{id}",
            new { id = RouteParameter.Optional });


        server = new HttpSelfHostServer(config);
        server.OpenAsync();
        MessageBox.Show("Serviço inicializado!");
        button2.Text = "Parar serviço";
    }
    else
    {
        server.CloseAsync();
        button2.Text = "Iniciar serviço";
    }
}

Get WebApi Jquery my MVC Site 获取WebApi Jquery我的MVC网站

function cmdLocal() {
    $.ajax({
        type: "GET",            
        dataType: "json",
        url: "http://localhost:8089/api/gps/",
        success: function(callback){
           alert(callback);
        },
        error: function(err){
            alert("You have a error"+err);
        }
    });         
}

Error Get cmdLocal, with Chrome 使用Chrome时错误获取cmdLocal

XMLHttpRequest cannot load http://localhost:8089/api/gps/. Origin http://localhost:36452 is not allowed by Access-Control-Allow-Origin. 

How to implement CORS in a Windows Forms application? 如何在Windows窗体应用程序中实现CORS?

tks ks

If the CORS support is implemented as a DelegatingHandler then there should be no difference between how it is used with Web Host versus self host. 如果将CORS支持实现为DelegatingHandler,则在Web Host与自托管之间的使用方式应该没有区别。 The only problem will come is if the CORS implementation is dependent on HttpContext . 唯一的问题是CORS实现是否依赖于HttpContext

What CORS implementation are you trying to use? 您尝试使用哪种CORS实施?

BTW your windows form app will need to run as admin for what you trying to do to work. 顺便说一句,您的Windows窗体应用程序将需要以管理员身份运行才能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM