locked
Get URL of requesting website in WEB API 2 RRS feed

  • Question

  • User-294614021 posted

    I need to get URL of requesting web site in My WEB API 2. I need to do this because of security. I want to response to the request which are coming from the URLs that are registered on my server.

    In Web API I have used

    1. Owin
    2. OAuth 2.0

    So far I have tried to get it from owin context and http context but no one gives URL. I am able to get IP address of the requesting web site.

    Any idea how can I achieve this?

    Wednesday, August 3, 2016 6:17 AM

Answers

  • User36583972 posted

    Hi varinderrai,

    At your WebAPI controller actions, you can use the following code to get requesting website urls.

        public string Post([FromBody]string par)
            {
                //get call rul
                //string url2= Request.RequestUri.ToString();
                //string baseUrl = Url.Request.RequestUri.GetComponents(
        UriComponents.SchemeAndServer, UriFormat.Unescaped);
                //get request website url
                string url3 = HttpContext.Current.Request.UrlReferrer.AbsoluteUri;
                // return (user.firstName.ToString() + " <> " + user.lastName);
                return "333" + par;
            }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 4, 2016 3:01 AM

All replies

  • User36583972 posted

    Hi varinderrai,

    At your WebAPI controller actions, you can use the following code to get requesting website urls.

        public string Post([FromBody]string par)
            {
                //get call rul
                //string url2= Request.RequestUri.ToString();
                //string baseUrl = Url.Request.RequestUri.GetComponents(
        UriComponents.SchemeAndServer, UriFormat.Unescaped);
                //get request website url
                string url3 = HttpContext.Current.Request.UrlReferrer.AbsoluteUri;
                // return (user.firstName.ToString() + " <> " + user.lastName);
                return "333" + par;
            }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 4, 2016 3:01 AM
  • User-294614021 posted

    Yohann, I tried with this but UrlReferrer is received null.

    Thursday, August 4, 2016 10:10 AM
  • User36583972 posted

    Hi  varinderrai,

    I have made a test on my side. It is working. I do not know why you can not get the URL of requesting website. I suggest you should make a test on your side and debug your code step by step.

    Also, if you have any error in your code, you can post a new thread in relative forums and share more relevant code/message to help us reproduce the problem.

    Best Regards,

    Yohann Lu

    Saturday, August 6, 2016 4:49 AM