在MVC 4的Windows Azure Request.Url错误的端口号 [英] Wrong port number in mvc 4 windows Azure Request.Url

查看:147
本文介绍了在MVC 4的Windows Azure Request.Url错误的端口号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个小asp.net MVC4 Web应用程序。项目模板在Visual Studio 2012年MVC蔚蓝4模板当我在剃刀鉴于Request.Url要求我得到错误的端口号。例如,如果我的网页的网址是

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 

我得到

http://127.0.0.1:82/mypage 

当我从Request.Url获得url。我在默认的蔚蓝模拟器我得到运行它的Visual Studio 2012

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?

推荐答案

找到了答案。本博客文章解释了原因非常好。

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

http://blogs.staykov.net/2013/ 05 / Windows的Azure的basicscompute-emulator.html

天青模拟器试图模拟真实环境尽可能。仿真器包括一个隐藏的负载平衡器(LB)。在LB角色试图绑定端口8​​1和失败(因为它已经被绑定),然后尝试绑定到82等(在我的情况下,它是成功的在端口82)。这种绑定后,Request.Url将提供LB网址。为了让你可以使用这个原基础URL

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

这篇关于在MVC 4的Windows Azure Request.Url错误的端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆