I am developing ASP.NET CORE 5 MVC project and I want to use server variables to send email and show the file on new tab. In my local machine I can get the host name but after publishing on IIS, I couldn't get the result.
I am getting base url like this
string baseUrl = string.Format("{0}://{1}", @HttpContext.Request.Scheme, @HttpContext.Request.HttpContext.GetServerVariable("HTTP_HOST"));
ViewBag.url = baseUrl;
I am using in razor page like
@if (@item.DOKUMAN1 == null)
{
<div class="col-sm-2 my-2">
<a hidden href="@ViewBag.url/UploadedImages/@item.DOKUMAN1" target="_blank">
<i class="icon ion-android-folder" style="font-size:25px;font-family:Calibri" src="~/UploadedImages/@item.DOKUMAN1"><b></b></i>
</a>
</div>
}
else
{
<div class="col-sm-2 my-2">
<a href="@ViewBag.url/UploadedImages/@item.DOKUMAN1" target="_blank">
<i class="icon ion-android-folder" style="font-size:25px;font-family:Calibri" src="~/UploadedImages/@item.DOKUMAN1"><b></b></i>
</a>
</div>
<input type="text" value="@ViewBag.url/UploadedImages/@item.DOKUMAN1"/>
}
I added images of debug results too.In local I can get the true result: http://localhost:16122/UploadedImages/1a78f896-6893-4079-a4d0-5a3387296ecb.mp4 on local server, but on IIS Server result is http:///UploadedImages/1a78f896-6893-4079-a4d0-5a3387296ecb.mp4.
How can I get the server base url ?
The following code snippet gives you the current host address :