Copied to clipboard

Flag this post as spam?

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


  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    Jul 15, 2016 @ 09:32
    Søren Kottal
    0

    Hi

    I am trying to make a 404 handler for media, so far I have come up with this:

    public class Media404Handler : INotFoundHandler
    {
        private int redirectId;
    
        public bool Execute(string url)
        {
            var urlParts = url.Split('/');
    
            if (urlParts.Contains("media"))
            {
                var qs = HttpContext.Current.Request.QueryString.ToString();
    
                HttpContext.Current.Response.Redirect("/Assets/img/404.png" + (string.IsNullOrWhiteSpace(qs) ? "?" + qs : ""));
    
                return true;
            }
            return false;
        }
    
    
        public bool CacheUrl
        {
            get { return false; }
        }
    
        public int redirectID
        {
            get { return redirectId; }
        }
    }
    

    When I try to browse to /media/test404 it gets run like it should and redirects to /Assets/img/404.png, but if I browse to /media/test404.jpg nothing happens.

    Is it just not possible to do this, or do I need to change the configuration somewhere?

  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    Jul 15, 2016 @ 10:05
    Søren Kottal
    101

    Got it working with an IIS rewrite - doesn't send a 404 status code, though. But thats ok in my case.

    <rewrite>
      <rules>
        <rule name="RewriteNonExistingImages">
          <match url=".((jpg)|(jpeg)|(png)|(gif))$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Rewrite" url="/Assets/img/404.png" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
    
  • Matt Cheale 8 posts 33 karma points
    Jun 23, 2017 @ 08:09
    Matt Cheale
    0

    I'll add my tuppence to this, as I got this working with IIS error handling by configuring "/media/web.config" with:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <handlers>
                <clear/>
                <add name="StaticFileHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.StaticFileHandler"/>
                <add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read"/>
            </handlers>
            <httpErrors errorMode="Custom" existingResponse="Replace">
                <remove statusCode="404" subStatusCode="-1" />
                <error statusCode="404" path="/404/" responseMode="ExecuteURL" />
            </httpErrors>
        </system.webServer>
        <system.web>
            <customErrors mode="Off" />
        </system.web>
    </configuration>
    

    Where "/404/" is my Umbraco not found page.

  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    Jun 23, 2017 @ 09:07
    Søren Kottal
    0

    Ooh, nice - going to have a look into that!

    Thanks for sharing!

Please Sign in or register to post replies

Write your reply to:

Draft