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 ?
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.
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
One of the solution would be to return the path of the url from the Logic layer ie 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.