Copied to clipboard

Flag this post as spam?

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


  • iNETZO 134 posts 497 karma points c-trib
    Nov 24, 2019 @ 15:12
    iNETZO
    0

    Error using Surfacecontroller with RouteTable.Routes.MapRoute

    Hi,

    I am trying to create a custom controller for handling media. I created a component with a custom route. My problem is that when I use a surface controller type controller, it gives the following error :

    [Exception: Failed to create an instance of controller type UmbracoMediaProtect.MediaProtect.Controllers.MediaProtectController (see inner exception).]
    
    [NullReferenceException: Object reference not set to an instance of an object.]
    

    When I use a controller of the type "Controler" the error does not occur but I want to add a [MemberAuthorize] attribute to the controller.

    Composer

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class MediaProtectComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Register<MediaProtectController>(Lifetime.Request);
            composition.Components().Append<MediaProtectComponent>();
        }
    }
    

    Component

    public class MediaProtectComponent : IComponent
    {
    
        public void Initialize()
        {
            RouteTable.Routes.RouteExistingFiles = true;
            RouteTable.Routes.MapRoute(
                "MediaProtect",
                "media/{id}/{file}",
                new
                {
                    controller = "MediaProtect",
                    action = "Index",
                    id = UrlParameter.Optional,
                    file = UrlParameter.Optional
                }
            );
        }
    
        public void Terminate()
        {
    
        }
    
    }
    

    Controller [MemberAuthorize] public class MediaProtectController : Controller {

        public ActionResult Index(string id, string file)
        {
            string mediaPath = $"/media/{id}/{file}";
            var media = Current.Services.MediaService.GetMediaByPath(mediaPath);
    
            if (media == null)
            {
                return HttpNotFound();
            }
    
            var fileStream = VirtualPathProvider.OpenFile(mediaPath);
            return new FileStreamResult(fileStream, MimeMapping.GetMimeMapping(file));
    
        }
    
    }
    

    Is it not possible to use a Surfacecontroller in combination with RouteTable.Routes.MapRoute?

    Best regards,

    iNETZO

  • Graham Davis 110 posts 376 karma points
    Nov 24, 2019 @ 21:26
    Graham Davis
    0

    Try changing

    public class MediaProtectController : Controller

    To

    public class MediaProtectController : Umbraco.Web.Mvc.SurfaceController

  • iNETZO 134 posts 497 karma points c-trib
    Nov 24, 2019 @ 22:06
    iNETZO
    0

    Thanks for your reply Graham! When i change it to a surfacecontroller, i get the error above:

    [Exception: Failed to create an instance of controller type UmbracoMediaProtect.MediaProtect.Controllers.MediaProtectController (see inner exception).]

    [NullReferenceException: Object reference not set to an instance of an object.]

  • iNETZO 134 posts 497 karma points c-trib
    Nov 25, 2019 @ 22:58
    iNETZO
    0

    Is there someone who used a surfacecontroller with a custom route succesfull in Umbraco 8?

  • Graham Davis 110 posts 376 karma points
    Nov 25, 2019 @ 23:42
Please Sign in or register to post replies

Write your reply to:

Draft