生成没有端口号的绝对URL的解决方法 [英] Workaround for generating absolute URLs without port number

查看:577
本文介绍了生成没有端口号的绝对URL的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我的网站托管在appHarbor上。当我访问一个页面时,它附加一个端口号与引用的URL如:http:/ /www.test.com/test.aspx?returnUrl=http://www.test.com:11111/abc/kgh.aspx。

它附加了11111端口号。我在那里coundnt转到那个页面。



我有一个方法但无法理解和使用它:



  public   static   string  ToPublicUrl( UrlHelper urlHelper,Uri relativeUri)
{
var httpContext = urlHelper.RequestContext.HttpContext;

var uriBuilder = new UriBuilder
{
Host = httpContext.Request.Url.Host,
Path = /
Port = 80
Scheme = http
};

if (httpContext.Request.IsLocal)
{
uriBuilder.Port = httpContext.Request.Url.Port ;
}

return new Uri(uriBuilder.Uri,relativeUri ).AbsoluteUri;
}







任何人都可以帮助我...



谢谢

解决方案

你需要覆盖这个函数。

这是ToPublicURL的完整类





使用System;

使用System.Web;

使用System。 Web.Mvc;



名称空间MileageStats.Web.Helpers

{

公共静态类UrlExtensions

{

//偶尔,我们需要生成一个可公开访问的URL。

//例如,在使用第三个进行身份验证的情况下派对我们需要提供

//第三方调用的返回URL。

//在某些情况下,例如使用处理服务提供商托管应用程序

//通过在不同端口上内部转发请求来加载余额,标准

// ge的方法在ASP.NET中编写URL可以使用其中一个内部端口。

// ToPublicUrl帮助我们创建一个可公开访问的URL。

公共静态字符串ToPublicUrl (这个UrlHelper urlHelper,字符串动作,字符串控制器)

{

var uri = new Uri(urlHelper.Action(action,controller),UriKind.Relative);

返回ToPublicUrl(urlHelper,uri);

}



公共静态字符串ToPublicUrl(此UrlHelper urlHelper,Uri relativeUri )

{

var request = urlHelper.RequestContext.HttpContext.Request;

返回ToPublicUrl(request,relativeUri);

}



公共静态字符串ToPublicUrl(此HttpRequestBase请求,Uri relativeUri)

{

var url = request.Url;

返回ToPublicUrl(url.Host,u rl.Scheme,url.Port,request.IsLocal,relativeUri);

}



公共静态字符串ToPublicUrl(此HttpRequest请求,Uri relativeUri)

{

var url = request.Url;

返回ToPublicUrl(url.Host,url.Scheme,url.Port,request .IsLocal,relativeUri);

}



//注意:这假设在不在本地运行时将始终使用端口80。 br />
私有静态字符串ToPublicUrl(字符串主机,字符串方案,int端口,bool isLocal,Uri relativeUri)

{

var uriBuilder = new UriBuilder

{

主机=主机,

路径=/,

端口= 80,

Scheme = scheme,

};



if(isLocal)

{

uriBuild er.Port = port;

}



返回新的Uri(uriBuilder.Uri,relativeUri).AbsoluteUri;

}



公共静态字符串UnencodedAction(此UrlHelper助手,字符串操作,字符串控制器,对象routeValues)

{

返回HttpUtility.UrlDecode(helper.Action(action,controller,routeValues));

}

}

}





参考链接

https://github.com/mspnp/mobile-web/blob/master/MileageStats.Web/Helpers/UrlExtensions.cs [<一个href =https://github.com/mspnp/mobile-web/blob/master/MileageStats.Web/Helpers/UrlExtensions.cs\"target =_ blanktitle =New Window> ^ ]

Hello All,

My Website is hosted on appHarbor.When I access a page it is appending a port no with the referred Url like:"http://www.test.com/test.aspx?returnUrl=http://www.test.com:11111/abc/kgh.aspx".
It is appending 11111 port no.There by I coundnt go to that page .

I have got a method but unable to understand and use it:

public static string ToPublicUrl(this UrlHelper urlHelper, Uri relativeUri)
   {
       var httpContext = urlHelper.RequestContext.HttpContext;

       var uriBuilder = new UriBuilder
       {
           Host = httpContext.Request.Url.Host,
           Path = "/",
           Port = 80,
           Scheme = "http",
       };

       if (httpContext.Request.IsLocal)
       {
           uriBuilder.Port = httpContext.Request.Url.Port;
       }

       return new Uri(uriBuilder.Uri, relativeUri).AbsoluteUri;
   }




Can anybody help me...

Thanks

解决方案

You need to override this function.
Here is the complete class for ToPublicURL


using System;
using System.Web;
using System.Web.Mvc;

namespace MileageStats.Web.Helpers
{
public static class UrlExtensions
{
// Occasionally, we need to generate a url that is publicly accessible.
// For example, in the case of authenticating with a third party we need to provide
// a return url for the third-party invoke.
// In some circumstances, such as hosting the app with a service provider that handles
// load balances by forwarding the request internally on different ports, the standard
// approach for generating a URL in ASP.NET may use one of the internal ports.
// ToPublicUrl helps us a create a URL that will be publically accessible.
public static string ToPublicUrl(this UrlHelper urlHelper, string action, string controller)
{
var uri = new Uri(urlHelper.Action(action, controller), UriKind.Relative);
return ToPublicUrl(urlHelper, uri);
}

public static string ToPublicUrl(this UrlHelper urlHelper, Uri relativeUri)
{
var request = urlHelper.RequestContext.HttpContext.Request;
return ToPublicUrl(request,relativeUri);
}

public static string ToPublicUrl(this HttpRequestBase request, Uri relativeUri)
{
var url = request.Url;
return ToPublicUrl(url.Host,url.Scheme,url.Port,request.IsLocal, relativeUri);
}

public static string ToPublicUrl(this HttpRequest request, Uri relativeUri)
{
var url = request.Url;
return ToPublicUrl(url.Host, url.Scheme, url.Port, request.IsLocal, relativeUri);
}

//Note: this assumeS that port 80 will always be used when not running locally.
private static string ToPublicUrl(string host, string scheme, int port, bool isLocal, Uri relativeUri)
{
var uriBuilder = new UriBuilder
{
Host = host,
Path = "/",
Port = 80,
Scheme = scheme,
};

if (isLocal)
{
uriBuilder.Port = port;
}

return new Uri(uriBuilder.Uri, relativeUri).AbsoluteUri;
}

public static string UnencodedAction(this UrlHelper helper, string action, string controller, object routeValues)
{
return HttpUtility.UrlDecode(helper.Action(action, controller, routeValues));
}
}
}


reference link
https://github.com/mspnp/mobile-web/blob/master/MileageStats.Web/Helpers/UrlExtensions.cs[^]


这篇关于生成没有端口号的绝对URL的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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