简体   繁体   English

为什么 IIS 服务器返回 ASP.NET CORE 5 MVC 项目的 null baseUrl,但在本地服务器上返回 true?

[英]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.我正在开发 ASP.NET CORE 5 MVC 项目,我想使用服务器变量发送 email 并在新选项卡上显示文件。 In my local machine I can get the host name but after publishing on IIS, I couldn't get the result.在我的本地机器上,我可以获取主机名,但是在 IIS 上发布后,我无法获取结果。

I am getting base url like this我这样得到基地 url

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我在 razor 页面中使用

                           @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 .我也添加了调试结果的图像。在本地我可以获得真实结果: http://localhost:16122/UploadedImages/1a78f896-6893-4079-a4d0-5a3387296ecb.mp4在本地服务器上,但在 IIS 服务器上结果是http: ///UploadedImages/1a78f896-6893-4079-a4d0-5a3387296ecb.mp4

Here is the result of local server这是本地服务器的结果在此处输入图像描述

Here is the result of IIS server这是 IIS 服务器的结果在此处输入图像描述

How can I get the server base url?我怎样才能得到服务器基地 url?

The following code snippet gives you the current host address:以下代码片段为您提供了当前的主机地址:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心 MVC<select><option>返回空值 - ASP.Net Core MVC <select> <option> returns null ASP.NET Core 6 MVC 项目标识 model 返回空 - ASP.NET Core 6 MVC project identity model returns empty 服务器在IIS6上使用asp.net 3.5返回空白页面 - Server returns blank pages with asp.net 3.5 on IIS6 无法在IIS服务器上部署ASP.NET MVC项目 - Unable to deploy asp.net mvc project on the IIS server ASP.NET 内核 Ajax 返回 null - ASP.NET Core Ajax returns null 为什么 Asp.Net Core Kestrel 服务器向 Ngrok 返回 404 并且控制器永远不会被调用? - Why is Asp.Net Core Kestrel server returns 404 to Ngrok and Controller is never gets called? ASP.NET MVC 5(非 MVC 核心)TempData 在 Azure AppService 环境之一中不起作用并返回 null 值 - ASP.NET MVC 5 (Not MVC Core) TempData is not working in one of the Azure AppService Environment and returns null value User.Identity.Name 在 ASP.NET Core MVC 应用程序中返回 null - User.Identity.Name returns null in ASP.NET Core MVC application 为什么Html.Checkbox(“Visible”)在ASP.NET MVC 2中返回“true,false”? - Why Html.Checkbox(“Visible”) returns “true, false” in ASP.NET MVC 2? 在 Asp.Net Mvc Core 中调用存储库模式方法返回 null - Calling Repository Pattern methods returns null in Asp.Net Mvc Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM