Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • BJ Patel 84 posts 210 karma points
    Feb 11, 2016 @ 18:41
    BJ Patel
    1

    Getting value of UmbracoContext.Current as null

    Hello,

    I am having the following code on my Home Controller / Viewmodel class

    public class HomeController : SurfaceController
    {
          ....
        [HttpGet]
        public ActionResult Index()
        {
    
          ......
            var user = new UserDetail() { FirstName = "FirstName", LastName = "LastName" };
            UserDetailViewModel userViewModel = new UserDetailViewModel() { User = user };
            return View(userViewModel);
        }
    }
    
      [AllowAnonymous]
      public ActionResult LoginIn()
      {
            var user = new UserDetail() { FirstName = "FirstName", LastName = "LastName" };
            UserDetailViewModel model = new UserDetailViewModel() { User = user };
            return PartialView("~/Views/Partials/__LoginPartial.cshtml", model);
    
        }
         ........
    }
    
    
    
    
    public class UserDetailViewModel : RenderModel
    {    
        public UserDetailViewModel() : this(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) { }
        public UserDetailViewModel(IPublishedContent content, CultureInfo culture) : base(content, culture) { }
        public UserDetailViewModel(IPublishedContent content) : base(content) { }
        public UserDetail User { get; set; }
    }
    

    And following is the ActionExcuteFilter

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public sealed class ActionExcuteFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (HttpContext.Current.Session == null || Convert.ToInt32(HttpContext.Current.Session["UserId"]) ==0)
            {
                var myAccountPage = uQuery.GetNodesByType("Login").FirstOrDefault();
    
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary {
                    { "action", "LoginIn" },
                    { "controller", "Home" }});
    
            }
            base.OnActionExecuting(filterContext);
        }
    }
    

    And on Home page have the following link

    http://localhost/Products?categoryId=1 Which refer to action of product controller.

    So if user get login, and click on link everything works fine. (Means to say getting the value from UmbracoContext and respective pages get rendered )

    But when user Click on the link without login trouble starts (get error while fetching the values from UmbracoContext )

    So the Execution is:

    Product controller is call > ActionExcuteFilter > RedirectToRouteResult > Home Controller >LoginIn Action > UserDetailViewModel > Gives error (getting UmbracoContext.Current as null)

    I have ref. many post like Getting CurrentPage from SurfaceController

    but not able to list out the solution .

    Thanks in advance

    BJ

  • Daniel 60 posts 174 karma points
    Feb 11, 2016 @ 19:03
    Daniel
    0

    Could you explain what you are trying to accomplish?

    This setup seems a bit overkill, unless you're trying to do something out of the ordinary :)

  • BJ Patel 84 posts 210 karma points
    Feb 11, 2016 @ 19:28
    BJ Patel
    0

    In single statement

    When I try to access (without login) .URL : http://localhost:/Product?categoryId=1

    getting following error.

    enter image description here

    Regards., BJ

  • Daniel 60 posts 174 karma points
    Feb 11, 2016 @ 19:32
    Daniel
    0

    Yes I see the error, but what are you trying to accomplish?

    I think there may be an easier path to what you're trying to do!

  • BJ Patel 84 posts 210 karma points
    Feb 11, 2016 @ 19:53
    BJ Patel
    0

    Hi Daniel,

    I am just trying to Inject a MVC project in to Umbraco.

    so what I need to accomplish is that when user try to access page ..Product?categoryId=1 without login he should be redirect to login page.

    Regards., BJ

  • Daniel 60 posts 174 karma points
    Feb 11, 2016 @ 20:02
    Daniel
    0

    Aha, I see!

    Is the page "Product" a page in Umbraco with a document type? (perhaps you could make use of the automatic routing and the Members helper)

  • BJ Patel 84 posts 210 karma points
    Feb 11, 2016 @ 20:23
    BJ Patel
    0

    Yes ,

    It is having the document type.

  • Daniel 60 posts 174 karma points
    Feb 11, 2016 @ 20:30
    Daniel
    0

    To make use of the autorouting / hijacking routes, create a controller called the same thing as the /Product page document type alias, which might be "productPage" / "ProductPage":

    public class ProductPageController : RenderMvcController
    {
        //The ActionResult method also has a convention that if you name this after your Template alias, it automatically hits this - so let's say your Template alias is "ProductPageView"
        public ActionResult ProductPageView(RenderModel model)
        {
            if (Membership.GetUser() == null) return Redirect("/");
            //or maybe:
            if (!Members.IsLoggedIn()) return Redirect("/"); 
    
    
            return CurrentTemplate(model);
        }
    }
    

    This seems like a simpler way to check!

    More info here: https://our.umbraco.org/documentation/reference/routing/custom-controllers

  • BJ Patel 84 posts 210 karma points
    Feb 12, 2016 @ 07:39
    BJ Patel
    0

    Thanks Daniel for response.

    But in my case when request is raise to ProductController cursor go for validating user and ActionExcuteFilter calls LoginIn (RedirectToRouteResult to LoginIn on Home controller)

    But I can find the value of UmbracoContext in Product constructor before calling ActionExcuteFilter.

    Regards., BJ

Please Sign in or register to post replies

Write your reply to:

Draft