Copied to clipboard

Flag this post as spam?

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


  • blackhawk 313 posts 1368 karma points
    Dec 20, 2017 @ 16:08
    blackhawk
    0

    How to check if stylesheet exists

    From within my razor template, Is there anyway I can check if the following stylesheet exists before it is called?

    Url.Content("~/css/my-styles.css")
    

    There may be times when I am pulling a template over into a new instance and the stylsheet is not carried over yet, or the stylesheet may be renamed.

    Thanks for any tips

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 20, 2017 @ 18:37
    Dennis Aaen
    0

    Hi Blackhawk

    Could this perhaps be a solution for you

    https://css-tricks.com/snippets/jquery/check-if-element-exists/

    Hope this helps,

    /Dennis

  • blackhawk 313 posts 1368 karma points
    Dec 20, 2017 @ 18:53
    blackhawk
    0

    No, that doesn't help. I would like to check if the stylesheet exist from the server side.

  • Alex Clavelle 13 posts 124 karma points
    Dec 20, 2017 @ 21:06
    Alex Clavelle
    0

    Does UmbracoContext.Application.Services.FileService help?

  • blackhawk 313 posts 1368 karma points
    Dec 21, 2017 @ 01:05
    blackhawk
    0

    Thanks Alex. I have not used this process yet. Would you happen to know of a guide that can walk me through the use of it?

    Thanks

  • John Bergman 483 posts 1132 karma points
    Dec 21, 2017 @ 02:45
    John Bergman
    100

    In the razor code, you can use server.mappath to get to the directory/file, and then use system.io.file.exists on the returned path.

  • blackhawk 313 posts 1368 karma points
    Dec 21, 2017 @ 04:38
    blackhawk
    0

    Nice!

    I got it working like this...

    @{
    
        if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/css/myTheme.min.css")))
        {
            <link rel="stylesheet" href="@Url.Content("~/css/myTheme.min.css")" />
        }
    
    }
    

    Thanks a lot!

Please Sign in or register to post replies

Write your reply to:

Draft