1: // Typical Autofac integration with ASP.NET MVC
2: public class MvcApplication : System.Web.HttpApplication, IContainerProviderAccessor
3: {
4: public IContainerProvider ContainerProvider
5: {
6: get
7: {
8: return _containerProvider;
9: }
10: }
11: }
12:
13: // Our Resolve Extension Method
14: public static class AutofacHelper
15: {
16: public static T Resolve <T> (this HttpContext source)
17: {
18: IContainerProviderAccessor cpa = (IContainerProviderAccessor)source.ApplicationInstance;
19: return cpa.ContainerProvider.RequestContainer.Resolve<T> ();
20: }
21: }
22:
23: // Resolving easy-mode
24: ICustomerService customerService = HttpContext.Current.Resolve<ICustomerService> ();
25:
26: // Example code-behind
27: public partial class Site : System.Web.Mvc.ViewMasterPage
28: {
29: protected string GetUserName ()
30: {
31: if (Request.IsAuthenticated)
32: {
33: ICustomerService customerService = HttpContext.Current.Resolve<ICustomerService> ();
34: Customer customer = customerService.GetCustomer (HttpContext.Current.User.Identity.Name);
35: if (customer != null && customer.IsRegistered)
36: {
37: return customer.FirstName + " " + customer.LastName;
38: }
39: }
40:
41: return string.Empty;
42: }
43: }
1 comment:
Hey,
Sory I'm not realy commenting on this subject. But i've red you're code on http://stackoverflow.com/questions/409213/asp-net-mvc-roles-views
(couldn't comment there for some reaseon...)
I'm a rails programmer but i'm on a asp project. So my question is
"how do you pass ViewUserControl ? from the view to the helper method", is this automatic? Anyways I couldn't get it to work. Here's you're chance to help a newbie from Belguim out! Thanx a million.
My code is you're code, maybe i'm doing something wrong with namespaces?
in view : if(IsAdmin())
in helper :public static bool IsAdmin(this ViewUserControl pg){
....
Error:
"No overload for method 'IsAdmin' takes '0' arguments"
what's the this in "this ViewUserControl pg"
Any advice?
Thanx!
Post a Comment