Get request origin in C# api controller

后端 未结 2 1569
不知归路
不知归路 2021-02-07 04:00

Is there a way how can I can get request origin value in the api controller when I\'m calling some api endpoint with ajax call?

For example I\'m making this call from w

相关标签:
2条回答
  • 2021-02-07 04:13

    What you're looking for is probably the origin-header. All modern browsers send it along if you're doing a cross domain request.

    In an ApiController you fetch it like so:

    if (Request.Headers.Contains("Origin"))
    {
        var values = Request.Headers.GetValues("Origin");
        // Do stuff with the values... probably .FirstOrDefault()
    }
    
    0 讨论(0)
  • 2021-02-07 04:16

    You can grab it from the API methods via current HTTP request headers collection:

      IEnumerable<string> originValues;
      Request.Headers.TryGetValue("Origin", out originValues)
    
    0 讨论(0)
提交回复
热议问题