Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:

C#
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
Posted
Comments
Henning Dieterichs 9-Jun-12 14:48pm    
And what is your question?
I don't understand your problem...
Maybe "Url-Encoding" is a point which could help you.

1 solution

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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900