umbraco MVC自定义路由使用url中的点



我在url umbraco MVC自定义路由中使用点时遇到问题。/logo/images/image.jpg?width=100给出以下错误:

[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

/徽标/images/image.jpg/?宽度=100

有效,但这对我来说不是一个好的解决方案。我已经尝试在webconfig 中添加这个

<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>

取自https://average-joe.info/allow-dots-in-url-iis/但它不起作用:(

我的自定义路线如下:

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);
}
}

我使用的是umbraco vs..7.4.3

UmbracModule忽略具有文件扩展名的Url,因此永远不会为包含文件扩展名在内的请求创建UmbracContext。

您可以使用UmbracoContext.EnsureContext创建上下文,但如果您在处理程序的FindContent方法中这样做,则会遇到此异常。这是由UmbracVirtualNodeRouteHandler第18行的一个过时变量引起的,该变量持有对空UmbracContext的引用,并且没有获取新创建的上下文。

以下是如何解决它的,这样我就可以在调用VirtualNodeRouteHandler之前调用EnsureContext。

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

请注意,它不是MapUmbracRoute,而是标准的MVC映射路由,以及一个标准的MVC IRouteHandler,它在返回UmbracVirtualNodeRouteHandler的实例之前调用EnsureContext。

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;
}
}

这不是一个理想的解决方案,但在陈旧的变量问题被合并到核心之前,这是一个有效的解决方案-我已经提交了一份PR来修复它

其他一些人也遇到了同样的问题http://issues.umbraco.org/issue/U4-9384

相关内容

  • 没有找到相关文章

最新更新