2016-06-07 14 views
1

Ich habe ein Problem mit einem Punkt in url Umbraco MVC benutzerdefinierte Routen. /logo/images/image.jpg?width=100 gibt folgende Fehler:Umbraco MVC benutzerdefinierte Routen mit einem Punkt in der URL

[NullReferenceException: Object reference not set to an instance of an object.] 
    Umbraco.Web.Mvc.UmbracoVirtualNodeByIdRouteHandler.FindContent(RequestContext requestContext, UmbracoContext umbracoContext) +18 
    Umbraco.Web.Mvc.UmbracoVirtualNodeRouteHandler.GetHttpHandler(RequestContext requestContext) +48 
    System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +11987058 
    System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +141 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +91 

/logo/images/image.jpg/?width=100

Works, aber das ist keine gute Lösung für mich. Ich habe versucht, in webconfig diese Zugabe

<location path="logo"> 
     <!-- This only applies it to the relevant path and keeps the protection in place for elsewhere --> 
     <system.web> 
      <httpHandlers> 
       <add path="/images/*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" /> 
      </httpHandlers> 
     </system.web> 
     <!-- Required for IIS 7.0+ --> 
     <system.webServer> 
      <modules runAllManagedModulesForAllRequests="true" /> 
      <validation validateIntegratedModeConfiguration="false" /> 
      <handlers> 
       <add name="ApiURIs-ISAPI-Integrated-4.0" path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" preCondition="integratedMode,runtimeVersionv4.0" /> 
      </handlers> 
     </system.webServer> 
    </location> 

von https://average-joe.info/allow-dots-in-url-iis/ genommen aber es wird nicht funktionieren :(

Meine individuelle Route wie folgt aussieht:

mit
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) 
    { 
     //custom route 
     RouteTable.Routes.MapUmbracoRoute(
     "images", 
     "logo/{action}/{key}", 
     new 
     { 
      controller = "Image", 
      key = UrlParameter.Optional, 



     }, 
     new ProductsRouteHandler(4884)); 
    } 
} 
public class ProductsRouteHandler : UmbracoVirtualNodeByIdRouteHandler 
{ 

    public ProductsRouteHandler(int realNodeId) : base(realNodeId) 
    { 
    } 

    protected override IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext, IPublishedContent baseContent) 
    { 
     return base.FindContent(requestContext, umbracoContext, baseContent); 
    } 
} 

Ich bin umbraco vs.7.4.3

+0

Ihre Beispiel-URL ist für eine .jpg-Dateierweiterung, Umbraco enthält eine Abhängigkeit von ImageProcessor, die die dynamische Größenanpassung von Bildern übernimmt und für Ihre Anforderungen geeignet ist? – Anth12

+0

ja Ich benutze ImageProcessor bereits im Imagecontroller, um die Größe von Bildern zu ändern. Die Bilder befinden sich außerhalb des Webservers und ich möchte, dass die Größenänderung ähnlich wie ImageResize.NET funktioniert. Dieser Code funktioniert, außer für das Routing wird die Verwendung eines Punktes am Ende nicht zulassen. –

Antwort

0

Das UmbracoModule ignores Urls with a file extension, so ein UmbracoContext wird nie für eine reques erstellt werden t enthält eine Dateiendung.

Sie können einen Kontext mit UmbracoContext.EnsureContext erstellen. Wenn Sie dies jedoch in der FindContent-Methode Ihres Handlers getan haben, tritt diese Ausnahme auf. Dies wird durch eine veraltete Variable auf line 18 des UmbracoVirtualNodeRouteHandler verursacht, die einen Verweis auf einen UmbracoContext-Nullwert enthält und den neu erstellten Kontext nicht abruft.

Das Folgende ist wie um es funktioniert, so dass ich EnsureContext aufrufen konnte, bevor der VirtualNodeRouteHandler aufgerufen wird.

var route = routes.MapRoute("RouteName", "some/url/file.ext", new 
{ 
    controller = "MyController", 
    action = "Index" 
} 
route.RouteHandler = new UrlWithExtensionHandler(); 

Hinweis sein nicht die MapUmbracoRoute, aber die Standard-MVC Karte Routen, und ein Standard MVC IRouteHandler die EnsureContext ruft, bevor eine Instanz eines UmbracoVirtualNodeRouteHandler zurück.

public class UrlWithExtensionHandler : IRouteHandler 
{ 
    #region Implementation of IRouteHandler 

    public IHttpHandler GetHttpHandler(RequestContext requestContext) 
    { 
     // init umbraco context 
     var httpContext = new HttpContextWrapper(HttpContext.Current); 

     UmbracoContext.EnsureContext(
      httpContext, 
      ApplicationContext.Current, 
      new WebSecurity(httpContext, ApplicationContext.Current), 
      UmbracoConfig.For.UmbracoSettings(), 
      UrlProviderResolver.Current.Providers, 
      false); 

     var handler = new UrlWithExtensionVirtualNodeRouteHandler(); 
     return handler.GetHttpHandler(requestContext); 
    } 

    #endregion 
} 

public class UrlWithExtensionVirtualNodeRouteHandler : UmbracoVirtualNodeRouteHandler 
{ 
    protected override IPublishedContent FindContent(RequestContext requestContext, 
      UmbracoContext umbracoContext) 
    { 
     return someIPublishedContent; 
    } 
} 

keine ideale Lösung, aber eine gültige Problemumgehung, bis das abgestandene Variable Problem wird in dem Kern verschmolzen - ich vorgelegt habe eine PR es zu beheben

Einige andere haben auch das gleiche Problem haben http://issues.umbraco.org/issue/U4-9384