4

I have a business logic layer in a C# project and I need to find a way to generate a url based on the base url that is running the web site.

For example, this is the url: http://localhost:56240/Management/Quiz.aspx?QuizID=46

I need a way to get this part: http://localhost:56240 using a C# code from the business logic layer (means I don't can't use the Request object or context.Request).

Is there a way to do that ?

Develop4Life
  • 7,581
  • 8
  • 58
  • 76
Liran Friedman
  • 4,027
  • 13
  • 53
  • 96
  • 1
    At some point, if you're handling a request, you must have access to the HttpContext. Get the context there and then pass whatever value you need to the BL as a value type so that the BL doesn't touch the HttpContext – frenchie May 19 '15 at 06:28
  • possible duplicate http://stackoverflow.com/questions/40680/how-do-i-get-the-full-url-of-the-page-i-am-on-in-c-sharp – The Reason May 19 '15 at 06:29
  • I know how to get the url when I have access to the HttpContext / Request. But is there a way to get it without having any access to it ? – Liran Friedman May 19 '15 at 06:34
  • That's what frenchie said - just *pass* it to the business layer. That's the whole point of separating the layers :) – Luaan May 19 '15 at 06:47

3 Answers3

4

From your class, you can use the HttpContext.Current property (in System.Web.dll). From there, you can use Request object as well. For example

HttpRequest request = HttpContext.Current.Request;
string url = request.Url.Authority.ToString();

Don't forget to include the reference for System.Web in your class.

Jay Jay Jay
  • 1,970
  • 2
  • 25
  • 47
Vikas Rana
  • 1,961
  • 2
  • 32
  • 49
  • 2
    This is certainly a way but now you have a tight dependency/coupling issue with System.Web in your BLL which means the BLL is not entirely reusable irrespective of the context of the application (i.e Web or services or Windows Service etc..) and its more tempting to do "webby" stuff by having access to the System.Web assembly in your BLL. Instead, I would create a helper class in the UI which simply gets the url and passes that into the BLL for it to perhaps return back or whatever. – Ahmed ilyas Jul 16 '15 at 18:20
  • But if we use `ConfigureAwait(false);` in API, `HttpContext` maybe null. – Farhad Zamani Feb 02 '22 at 09:34
0

call the method from presentation layer and pass HttpContext to business logic layer, you can use HttpContext.Request.Url.Authority to get your domain http://localhost:56240

or you can directly pass Request.Url.Authority as a String to your method if you don need others thing in HttpContext

Yu Yenkan
  • 745
  • 1
  • 9
  • 32
  • I know how to get the url when I have access to the HttpContext / Request. But is there a way to get it without having any access to it ? – Liran Friedman May 19 '15 at 06:35
  • hardcode, at least i don know any others way you can get your web domain without a `Page.Context`. @LiranFriedman why you need it without having any access to it – Yu Yenkan May 19 '15 at 06:38
0

One of the solution would be to return the path of the url from the Logic layer i.e without host. And append the host at the controller level.

From Logic

return new LogicResponseObject() {
...
Path = "/test/path/"
...
};

At Controller/Web/Service layer

HttpRequest request = HttpContext.Current.Request; //Get request object
string authority = request.Url.Authority; //http://www.example.com
string url = authority + logicResponseObject.Path; //http://www.example.com/test/path/

This way the logic will be de coupled with HTTP context object.

nak
  • 846
  • 2
  • 10
  • 26