Wrong port number in mvc 4 windows Azure Request.Url

余生长醉 提交于 2019-12-10 19:24:11

问题


I am writing a small asp.net MVC4 web application. The project template is azure mvc 4 template in visual studio 2012. When I request for Request.Url in razor view I get the wrong port number. For example if my webpage url is

http://127.0.0.1:81/mypage 

I get

http://127.0.0.1:82/mypage 

when I get the url from Request.Url. I am running it in the default azure emulator I get with visual studio 2012.

Why the port number is wrong? How to correct it?


回答1:


Found the answer. This blog article explains the reason very well.

http://blogs.staykov.net/2013/05/windows-azure-basicscompute-emulator.html

Azure emulator tries to emulate the real environment as much as possible. The emulator includes a hidden load balancer (LB). The LB roles tries bind port 81 and fails (because it is already bound) and then try to bind to 82 and so on(in my case it was successful on port 82). After this bind, Request.Url will provide the LB Url. To get the original base url you can use this

var request = Request.RequestContext.HttpContext.Request;

string baseUrl = request.UrlReferrer.Scheme + "://" +
             request.UserHostAddress + ":" +
             request.UrlReferrer.Port; 

It can be used directly inside Razor syntax



来源:https://stackoverflow.com/questions/21062617/wrong-port-number-in-mvc-4-windows-azure-request-url

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