mobile - View Switcher for ServiceStack? -
in mvc, there's viewswitcher, , can add _layout, _layout.mobile; myview , optional myview.mobile
what's best way accomplish in servicestack razor view? thanks
servicestack doesn't implicitly switch layouts @ runtime, instead preferred layout needs explicitly set. servicestack's razorrockstars demo website explains how dynamically switch views, i.e:
change views , layout templates @ runtime
the above convention overrideable can change both view , layout template used @ runtime returning response inside decorated httpresult:
return new httpresult(dto) { view = {viewname}, template = {layoutname}, };
this useful whenever want display same page in specialized mobile , print preview website templates. can let client change view , template gets used attributing service clientcanswaptemplates request filter attribute:
[clientcanswaptemplates] public class rockstarsservice : restservicebase { ... }
which simple implementation shows can can swap view or template used inside request filter:
public class clientcanswaptemplatesattribute : requestfilterattribute { public override void execute(ihttprequest req, ihttpresponse res, object requestdto) { req.items["view"] = req.getparam("view"); req.items["template"] = req.getparam("template"); } }
this attribute allows client change view gets used view , template querystring or formdata request params. live example of feature used change /rockstars page:
- /rockstars?view=angularjs
- /rockstars?template=simplelayout
- /rockstars?view=angularjs&template=simplelayout
changing layout used inside view
you can change layout used setting layout property inside razor view, e.g:
@inherits viewpage<response> @{ layout = ismobilerequest(base.request) ? "_layoutmobile" : "_layout"; }
Comments
Post a Comment