Why IIS Server returns null baseUrl of ASP.NET CORE 5 MVC project, but returns true on local server?

562 Views Asked by At

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;

result of ViewBag.url

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"/>
                            }

enter image description here

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.

Here is the result of local server enter image description here

Here is the result of IIS server enter image description here

How can I get the server base url ?

1

There are 1 best solutions below

1
On

The following code snippet gives you the current host address :

string myDomainName =HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);