Url.Action in controller generates port twice

别等时光非礼了梦想. 提交于 2021-02-10 12:56:51

问题


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 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

and appends port number 49518 twice. What could be the possible solution to this? Why this is happening?


回答1:


Just replace HttpContext.Request.Url.Authority with HttpContext.Request.Url.Host.

Because :

  • 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.

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.



来源:https://stackoverflow.com/questions/35586598/url-action-in-controller-generates-port-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!