0

Is there any way by which I can replace http://localhost:12345 while calling the WebAPI controller?

For Eg: For calling Controller we create URL like http://localhost:12345/api/Home?id=5 .. I do not want that.

I want my code to create http://localhost:12345 for me as string.

I had look around and I got this string baseURL = HttpContext.Current.Request.Url.Host; but it is only giving me localhost as an string.

In this statement Uri uri = new Uri("http://localhost:12345/api/Home?id=5"); we are passing URL to call controller. In my case, I want my this http://localhost:12345 part of URL should be created dynamically. Can anyone suggest other way around?

Thank You

Pro-n-apps
  • 55
  • 12
  • 1
    I'm not sure what you mean by "I want my code to create http://localhost:12345 for me as string." Perhaps you could post some sample code or explain the problem you're trying to solve – Nick.Mc Jan 17 '18 at 06:59
  • You can retrieve the entire request URL and then parse the host including the poert number (:12345) – DaniDev Jan 17 '18 at 07:34
  • Possible duplicate of [.NET - Get protocol, host, and port](https://stackoverflow.com/questions/21640/net-get-protocol-host-and-port) –  Jan 17 '18 at 07:42
  • @yildizm85 Read requirement correctly. – Pro-n-apps Jan 17 '18 at 21:47
  • @DaniDev any Example? – Pro-n-apps Jan 17 '18 at 21:53
  • @Nick.McDermaid Here is sample for You HttpClient cons = new HttpClient(); cons.BaseAddress = new Uri("http://localhost:12345/"); HttpResponseMessage res = cons.GetAsync("api/FormData?formNumber=" + formNumber).Result; above code snippet shows an example to make an call to controller of localhost. As you can see in con.BaseAddress I hard coded Base Address, rather than that I want code that will create base address for me. Thank You – Pro-n-apps Jan 17 '18 at 21:54

1 Answers1

0

This did my Job.

        var request = HttpContext.Current.Request;
        var builder = new UriBuilder(request.Url.Scheme, request.Url.Host, request.Url.Port);
        return builder.ToString();
Pro-n-apps
  • 55
  • 12
  • Nice @Pro-n-apps ! I trusted that you'd be able to figure it out without an Example ;-) – DaniDev Jan 17 '18 at 23:50
  • I am Confused actually, because will it run correctly after we host it on server. for Eg: Right now I have URL like http://localhost:12345/api/FormData?id=5 After hosting this project to server URL will be abc.XYZ.com/api/FormData?id=5 My confusion is will it able to handle the request the same way even after hosting on server? – Pro-n-apps Jan 18 '18 at 00:04
  • and what is the resulting string? – DaniDev Jan 18 '18 at 00:10
  • on the server, I mean :-) . The reason why I am asking is because it works on the server were the default port is likely 80 so I was wondering how UriBuilder handles it? Whether it shows up in the browser or not? other than that there shouldn't be anything puzzling about the fact that it works. – DaniDev Jan 18 '18 at 00:23
  • I didnt checked it on server yet, that will be done by TL. Is this the only way or there is other way around? – Pro-n-apps Jan 18 '18 at 01:03
  • Oh I see. Misread your post. thought you already tested on server. where is the request coming from? and to what purpose are youneeding the host string? since you are executing this (parsing) in the same context (in this case "localhost:12345") then it would seem that you are trying to generate another request URL (AJAX ?) If so you could just make it relative. Or you could assign a configuration hard coded value that would change in each deployment. – DaniDev Jan 18 '18 at 01:28
  • Currently, Server and Client are on same machine that's why localhost:12345 will work on any case but when WebAPI project will host on server machine then I do not think, the solution I got will work in that case. Because from client side I will make an AJAX call like with url like abc.xyx.com/api/FormData?formNumber=12455 – Pro-n-apps Jan 18 '18 at 03:18
  • "I will make an AJAX call" => "Or you could assign a configuration hard coded value that would change in each deployment" By configuration I mean in web.config or in a common JavaScript file. – DaniDev Jan 18 '18 at 07:00