dxa-web-application-dotnet
dxa-web-application-dotnet copied to clipboard
Support URLs which don't map to a Publication in CM
When someone requests a URL on a DXA webapplication which cannot be mapped to a publication, this error message is rendered: No matching Localization found for URL 'http://hostname/path'
.
There are two issues with this
- The error message is not styled; DXA has styled 500 error and 404 Not found pages, these are used for most error situations, but when the publication mapping does not work the webstite visitor gets this un styled error.
- The controllers are never hit, so you cannot have a controller handling any unmapped URLs. For example, I have a site which does not have any publication mapped to the root url at
http://hostname/
, instead all publications are mapped to/en
,/nl
and so on. I would like to make a controller which redirects the visitor from the root URLhttp://hostname/
tohttp://hostname/en
but the request will never hit my controller.
The problem is caused by Sdl.Web.Mvc.Statics.StaticContentModule
. This module serves static content items like media. It needs the site localization to figure out if the request URL is a static URL. When the localization cannot be found it will return a 404 not found status with the exception message.
I think the StaticContentModule should not render this error message. It could silently fail, write a warning to the log like it does today, and continue. The request will then go to the routes, eventually be handled by the PageController (or my custom controller in my use case). The PageController will also not find the site localization, and it can return a static 404 not found just like it can serve a static 500 error.
Your idea to handle the unknown Localization error later in the process makes sense, but note that there are two work-arounds available:
1.Ensure that there is a mapping for http://hostname
too in TTM (e.g. to some parent Publication which you don't really publish).
2. Implement your own "Unknown Localization Handler", i.e. a class which implements IUnknownLocalizationHandler
and is registered in Unity.config
. Such a handler will be invoked if a Localization cannot be resolved and it could return an HTTP 302 redirect.
I have run an experiment which does not throw an exception when there is no Localization set, but continues in a graceful way. https://github.com/jhorsman/dxa-web-application-dotnet/commits/localiation-experiment
TSI-2341 (Internal issue ID for tracking purposes)