i trying migrate mvc 5 application asp.net 5 mvc 6 (beta 7). having problems when using @inherits , @model directive together. works fine when used separately.
in _viewimports added @inherits directive use base page custom user properties.
public abstract class baseviewpage<tmodel> : razorpage<tmodel> { protected myprincipal appuser { { return new myprincipal(this.user claimsprincipal); } } } _viewimports.cshttml
@inherits commonweb.baseviewpage<tmodel> @addtaghelper "*, microsoft.aspnet.mvc.taghelpers" and can go appuser. in views. works if dont use typed view. if add @model directive in view inherited view page goes away.
help appreciated
update:
i did using custom pagebasetype in web.config in prior versions.
workaround.
public class viewhelper { viewcontext _context; public viewhelper(viewcontext context) { _context = context; } public myprincipal appuser { { return new myprincipal(_context.httpcontext.user claimsprincipal); } } public string controllername { { return _context.routedata.values["controller"].tostring(); } } } view:
@{ var viewhelper = new viewhelper(viewcontext);} a way achieve views?
there better way in mvc 6, supports injecting dependencies on views @inject directive. (the directive @inject ifoo foo allows use in view property named foo of type ifoo)
create new interface iappuseraccessor getting app user, example:
public interface iappuseraccessor { myprincipal getappuser(); } create class appuseraccessor implementing it:
public class appuseraccessor : iappuseraccessor { private ihttpcontextaccessor httpcontextprovider; public appuseraccessor(ihttpcontextaccessor httpcontextprovider) { this.httpcontextprovider = httpcontextprovider; } public myprincipal getappuser() { return new myprincipal ( httpcontextprovider.httpcontext.user claimsprincipal); } } register new interface in services container adding new entry in configureservices method of startup.cs:
services.addtransient<iappuseraccessor, appuseraccessor>(); finally use @inject directive inject iappuseraccessor in views. if add directive in viewimports.cshtml available on every view.
@inject webapplication4.services.iappuseraccessor appuseraccessor with pieces above can use on view(s):
@appuseraccessor.getappuser() update
if need inspect route values, controller name, can inject iactioncontextaccessor class , use follows:
public appuseraccessor(ihttpcontextaccessor httpcontextprovider, iactioncontextaccessor actioncontextaccessor) { this.httpcontextprovider = httpcontextprovider; this.actioncontextaccessor = actioncontextaccessor; } ... public string controllername { { return actioncontextaccessor.actioncontext.routedata.values["controller"].tostring(); } } of course, doesn't appuseraccessor anymore , smells has different responsabilities. @ least needs more appropriate name :)
i double check need controller name for. there might better way accomplish objective. (for example, if need generating new links/urls might use iurlhelper)
accessing viewcontext
looks beta8 has added support injecting viewcontext, although implementation details may change before rc. see this question
Comments
Post a Comment