Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bjarne Fyrstenborg 1098 posts 2906 karma points MVP 2x c-trib
    Sep 13, 2018 @ 11:38
    Bjarne Fyrstenborg
    0

    Get hostname inside UmbracoApiController

    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?

    /Bjarne

  • Shannon Deminick 1446 posts 4869 karma points hq
    Sep 13, 2018 @ 11:51
    Shannon Deminick
    0

    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.

  • Bjarne Fyrstenborg 1098 posts 2906 karma points MVP 2x c-trib
    Sep 13, 2018 @ 12:03
    Bjarne Fyrstenborg
    0

    Hi Shannon

    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?

    enter image description here

  • Shannon Deminick 1446 posts 4869 karma points hq
    Sep 13, 2018 @ 12:23
    Shannon Deminick
    0

    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:

    @UmbracoContext.PublishedContentRequest.....

  • Bjarne Fyrstenborg 1098 posts 2906 karma points MVP 2x c-trib
    Sep 13, 2018 @ 12:34
    Bjarne Fyrstenborg
    0

    Thanks for the explanation :)

    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;
    
  • Shannon Deminick 1446 posts 4869 karma points hq
    Sep 13, 2018 @ 12:40
    Shannon Deminick
    1

    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?

    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

  • Bjarne Fyrstenborg 1098 posts 2906 karma points MVP 2x c-trib
    Sep 13, 2018 @ 12:44
    Bjarne Fyrstenborg
    0

    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.

  • Shannon Deminick 1446 posts 4869 karma points hq
    Sep 13, 2018 @ 12:59
    Shannon Deminick
    100

    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.

  • Bjarne Fyrstenborg 1098 posts 2906 karma points MVP 2x c-trib
    Sep 13, 2018 @ 13:11
    Bjarne Fyrstenborg
    0

    Thanks for the examples.

    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?

  • Shannon Deminick 1446 posts 4869 karma points hq
    Sep 13, 2018 @ 15:02
    Shannon Deminick
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft