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?
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.]
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 :
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
Component
Controller [MemberAuthorize] public class MediaProtectController : Controller {
Is it not possible to use a Surfacecontroller in combination with RouteTable.Routes.MapRoute?
Best regards,
iNETZO
Try changing
public class MediaProtectController : Controller
To
public class MediaProtectController : Umbraco.Web.Mvc.SurfaceController
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.]
Is there someone who used a surfacecontroller with a custom route succesfull in Umbraco 8?
See my reply on this post:
https://our.umbraco.com/forum/using-umbraco-and-getting-started/99997-how-do-i-set-up-a-custom-route-in-umbraco-8
is working on a reply...