简体   繁体   中英

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

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;

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

在此处输入图像描述

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在此处输入图像描述

Here is the result of IIS server在此处输入图像描述

How can I get the server base url?

The following code snippet gives you the current host address:

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM