Adaptive web site should adjust his behaivior according to current device. Adaptive site should also have preference mechanism, with ability to switch between preferences, not matter for device (like github for example). This could help a lot of developers to build rich and adaptive websites.
Firstly we need to include MvcDeviceDetector
package via .
Install-Package MvcDeviceDetector -Pre
Than we should register required service and specify some options: You could just add device detector:
services.AddDeviceDetector();
or you may implement you own IDeviceFactory
for device detection.
And optionally you may add device switcher (for runtime device changing).
services.AddDeviceSwitcher<CookieSwitcher>();
or
services.AddDeviceSwitcher<UrlSwitcher>();
or implement your own device switcher by implementing interface IDeviceSwitcher
After this configuration new ViewLocationExpanders will be applied and start to search device specific views from prefixes or subfolders. Device would be detected from user agent according to lite device detection mechanism (simillar to http://projects.spring.io/spring-mobile/).
You could also add preference switcher mechanism, to enable switch:
services.AddDeviceSwitcher<UrlSwitcher>(switcherOptions => { }, deviceOptions => { });
If you specify device switcher, you should also map this switcher to some route:
route.MapDeviceSwitcher();
After that, you will have an ability to override default agent detetection with some preference, by calling URL in format: /choose/{device}
.
Mechanism provide following extensibility point:
- Allow developer to change device codes, and aliases
- Allow developer to use own device implementation, by adding specific device factory
- Allow developer to introduce new device types expect regular tablet and mobile
- Allow developer to switch between
Switcher
mechanism (by default, it should store preference in cookies, but also should be have ability to use URL switcher or whatever machanism that you could implement) - Allow developer to change URL generation rules
For any questions and bug please file an issue. Any contributions highly appreciate.