Skip to content

Instantly share code, notes, and snippets.

@sshibani
Created November 9, 2016 19:54
Show Gist options
  • Save sshibani/90ff5c48ddb1f86ab84214347940b3b3 to your computer and use it in GitHub Desktop.
Save sshibani/90ff5c48ddb1f86ab84214347940b3b3 to your computer and use it in GitHub Desktop.
public class PublicationResolver : IPublicationResolver
{
private readonly DynamicMappingsRetriever _mappingsRetriever;
public PublicationResolver()
{
_mappingsRetriever = new DynamicMappingsRetriever();
}
public int ResolvePublicationId()
{
return this.Resolve().PublicationId;
}
private int Resolve()
{
var url = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path);
int espaceIndex = url.IndexOf("%");
if (espaceIndex > 0)
{
url = url.Substring(0, espaceIndex);
}
try
{
var mapping = _mappingsRetriever.GetPublicationMapping(url);
if (mapping == null || (!mapping.Port.IsNullOrEmpty() && mapping.Port != uri.Port.ToString()))
{
throw new Exception("publicationId not found");
}
return mapping.PublicationId;
}
catch (Exception ex)
{
throw new Exception("publicationId not found", ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment