Inside UmbracoApiController you can access Umbraco and UmbracoContext. To get the hostname I can use one of the following. Not sure if one of them is preferable?
var domainName = UmbracoContext.HttpContext.Request.Url.Host;
or..
var domainName = UmbracoContext.PublishedContentRequest.DomainUri.Host;
however I can't use this since Request is null (not sure if it is a bug?)
var domainName = Request.Url.Host;
Besides that is it bad practise to use UmbracoHelper/Umbraco or UmbracoContext inside a static method or static class as long it doesn't reference to a static reference of UmbracoHelper?
I have a class inheriting from UmbracoApiController and a method which use Request.Url.Host:
public class MyHelper : UmbracoController
{
public int GetRootIdFromDomain()
{
var domainName1 = UmbracoContext.HttpContext.Request.Url.Host;
var domainName2 = UmbracoContext.PublishedContentRequest.DomainUri.Host;
var domainName3 = Request.Url.Host;
return GetRootIdFromDomain(domainName1);
}
}
An from a view request this:
@{
var myHelper = new MyHelper();
var rootId = myHelper.GetRootIdFromDomain();
}
domainName1 and domainName2 both works and return the expected domain name, but it breaks on domainName3 because Request object is null, but I don't know why?
A helper shouldn't inherit from a controller. A controller should just be a used as a controller with routable actions . A controller will be constructed and executed in the context of a request when it's matched for routing but you aren't using this controller for that, you are just using this controller as a base class. It just so happens that the UmbracoContext of the controller is wired up in this case
You have access to the request directly in your view:
@Request.Url.Host
You also have access to the UmbracoContext directly in your view:
Actually we originally had this class as a static class like this:
public static class MyHelper
{
/// Get root id from domain name
/// </summary>
/// <returns>int</returns>
public static int GetRootIdFromDomain()
{
var domainName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
var domainName = Request.Url.Host;
return GetRootIdFromDomain(domainName);
}
}
Are there alternatives to get Request inside a static method in this case? Is it bad practice to use the following inside this static method?
var domainName = UmbracoContext.Current.HttpContext.Request.Url.Host;
Why not pass in the context to the method? or better yet make this an extension method?
public static int GetRootIdFromDomain(this UmbracoContext umbCtx)
{
var domainName = umbCtx.HttpContext.Request.ServerVariables["SERVER_NAME"];
var domainName = Request.Url.Host;
return GetRootIdFromDomain(domainName);
}
then you can just do
@UmbracoContext.GetRootIdFromDomain();
and you aren't using any singletons. I don't know what GetRootIdFromDomain is doing though, perhaps that is using singletons in there too?
Basically if you can avoid singletons, please don't use them and the fact is you generally don't need to use them because all of the services you need are all exposed on all of the things you normally use like Views. So just use what you have and if you need to pass them to methods then do it and/or use extension methods
Furthermore should I get the Url from HttpContext or PublishedContentRequest?
Request doesn't see to be available directly inside a static class/method?
I could make it as an extension method, but I wonder if it end up with a lot of methods on UmbracoContext? alternative I could pass in the UmbracoContext as a parameter.
A static class doesn't define anything so it only know about what you've defined or what you pass it which is why Request, etc... isn't there.
I didn't mean to leave the line in there: var domainName = Request.Url.Host; .. that was a mistake
I'd stick to getting the Url from HttpContext. It's probably the same Url instance as PublishedContentRequest but that's a more specialized class that you don't really need to access.
An extension method is just 'syntactic sugar', Doing this
public static int GetRootIdFromDomain(this UmbracoContext umbCtx)
or this
public static int GetRootIdFromDomain(UmbracoContext umbCtx)
compiles to the same thing, however the extension method syntax lets you call it like:
@UmbracoContext.GetRootIdFromDomain();
whereas the other one would require you to call it like
@MyHelper.GetRootIdFromDomain(UmbracoContext)
Even by defining the extension method, you can still call it with the MyHelper.GetRootIdFromDomain syntax. Just depends on the syntax you prefer.
So from UmbracoContext I can use one of these. Is one of these preferable to use?
public static int GetRootIdFromDomain(this UmbracoContext umbCtx)
{
var domainName = umbCtx.PublishedContentRequest.DomainUri.Host;
var domainNameAlt1 = umbCtx.HttpContext.Request.Url.Host;
var domainNameAlt2 = umbCtx.HttpContext.Request.ServerVariables["SERVER_NAME"];
return GetRootIdFromDomain(domainName);
}
I guess via HttpContext it is the same as Request in a view, where Request is of type HttpRequestBase?
I'd go with umbCtx.HttpContext.Request.Url.Host; but you could also just make this method an extension method of Request since you have that in your view and then you don't need to go via the UmbracoContext to get to it
Get hostname inside UmbracoApiController
Inside UmbracoApiController you can access
Umbraco
andUmbracoContext
. To get the hostname I can use one of the following. Not sure if one of them is preferable?or..
however I can't use this since
Request
is null (not sure if it is a bug?)Besides that is it bad practise to use UmbracoHelper/Umbraco or UmbracoContext inside a static method or static class as long it doesn't reference to a static reference of
UmbracoHelper
?/Bjarne
Where are you trying to use
Request.Url.Host;
?In the constructor or something? Request can never be null inside of a webapi action, but that is what you should use.
Hi Shannon
I have a class inheriting from
UmbracoApiController
and a method which useRequest.Url.Host
:An from a view request this:
domainName1
anddomainName2
both works and return the expected domain name, but it breaks ondomainName3
becauseRequest
object is null, but I don't know why?A helper shouldn't inherit from a controller. A controller should just be a used as a controller with routable actions . A controller will be constructed and executed in the context of a request when it's matched for routing but you aren't using this controller for that, you are just using this controller as a base class. It just so happens that the
UmbracoContext
of the controller is wired up in this caseYou have access to the request directly in your view:
@Request.Url.Host
You also have access to the UmbracoContext directly in your view:
@UmbracoContext.PublishedContentRequest.....
Thanks for the explanation :)
Actually we originally had this class as a static class like this:
Are there alternatives to get
Request
inside a static method in this case? Is it bad practice to use the following inside this static method?It is always preferable to not use singletons :)
Why not pass in the context to the method? or better yet make this an extension method?
then you can just do
and you aren't using any singletons. I don't know what
GetRootIdFromDomain
is doing though, perhaps that is using singletons in there too?Basically if you can avoid singletons, please don't use them and the fact is you generally don't need to use them because all of the services you need are all exposed on all of the things you normally use like Views. So just use what you have and if you need to pass them to methods then do it and/or use extension methods
Furthermore should I get the
Url
fromHttpContext
orPublishedContentRequest
?Request
doesn't see to be available directly inside a static class/method?I could make it as an extension method, but I wonder if it end up with a lot of methods on
UmbracoContext
? alternative I could pass in the UmbracoContext as a parameter.A static class doesn't define anything so it only know about what you've defined or what you pass it which is why Request, etc... isn't there.
I didn't mean to leave the line in there:
var domainName = Request.Url.Host;
.. that was a mistakeI'd stick to getting the Url from HttpContext. It's probably the same Url instance as PublishedContentRequest but that's a more specialized class that you don't really need to access.
An extension method is just 'syntactic sugar', Doing this
or this
compiles to the same thing, however the extension method syntax lets you call it like:
whereas the other one would require you to call it like
Even by defining the extension method, you can still call it with the MyHelper.GetRootIdFromDomain syntax. Just depends on the syntax you prefer.
Thanks for the examples.
So from
UmbracoContext
I can use one of these. Is one of these preferable to use?I guess via
HttpContext
it is the same asRequest
in a view, whereRequest
is of typeHttpRequestBase
?I'd go with
umbCtx.HttpContext.Request.Url.Host;
but you could also just make this method an extension method ofRequest
since you have that in your view and then you don't need to go via the UmbracoContext to get to itis working on a reply...