In C # MVC4 how to get server path?

In C # MVC4 how to get the server path. for example: http://192.169.1.120:60632

Is there a helper function that can convert something like ~/aFolder/file.htm to an absolute path? Ideally, I would like to use any url and convert it to a complete absolute url. For instance. can handle ..

And it will work anywhere inside the C # code, that is, in the action controller, in the signalR concentrator, model, etc.

And it will work when I deploy to a remote server.

+4
source share
3 answers

Use the Request.Url property available in the Controller . This returns a Uri object containing request information. From there, you can access the AbsoluteUri and Port properties to get the necessary information.

If you are interested in getting URL information from SignalR, try looking at this question and answer .

+3
source

Try it,

System.Web.HttpContext.Current.Server.MapPath(@"~/FolderName")

You can also try them

string path = AppDomain.CurrentDomain.GetData("FolderName").ToString(); HostingEnvironment.MapPath(@"~/FoldeName");

+1
source

In MVC, you can get the full URL using the fourth parameter of Url.Action - protocol :

 Url.Action("Index", "Home", null, Request.Url.Scheme) 
0
source

Source: https://habr.com/ru/post/1498829/


All Articles