问题描述
我正在使用下面的代码来生成完全合格的 url
,并将其作为 json
传回以进行重定向.
I am using below piece of code to generate a fully qualified url
and pass it back as json
for redirection.
returnUrl = Url.Action("ActionName", "Controller",
new RouteValueDictionary(new { type= returnUrl }),
HttpContext.Request.Url.Scheme,
HttpContext.Request.Url.Authority);
returnUrl
最初将具有 type1
或 type2
值,这就是为什么我将类型指定为 returnUrl
的原因然后将其值替换为生成的 url
,但它会生成
returnUrl
will initially have a value either type1
or type2
which is why I have given type as returnUrl
and then replacing its value with a generated url
, but it generates
http://localhost:49518:49518/Controller/ActionName?type=type1
//^^^^^ Extra port added
,并将端口号 49518
附加两次.有什么可能的解决方案?为什么会这样?
and appends port number 49518
twice. What could be the possible solution to this? Why this is happening?
推荐答案
只需将 HttpContext.Request.Url.Authority
替换为 HttpContext.Request.Url.Host
.
因为:
-
HttpContext.Request.Url.Authority
返回服务器的域名系统(DNS)主机名或IP地址和端口号. -
HttpContext.Request.Url.Host
返回服务器的DNS主机名或IP地址.
HttpContext.Request.Url.Authority
returns the Domain Name System (DNS) host name or IP address and the port number for a server.HttpContext.Request.Url.Host
returns the DNS host name or IP address of the server.
在您的代码中,您使用的是 Url.Action
的重载,该重载接受主机名而不是包含端口的权限.
In your code you are using an overload of Url.Action
that accept the host name instead of the authority which contains the port.
使用此修复程序,您的端口将被自动添加,并且不会出现端口重复.
With this fix your port will be automatically added and there will be not port duplication.
这篇关于控制器中的Url.Action两次生成端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!