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);
}
}
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);
}
}
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.
Getting value of UmbracoContext.Current as null
Hello,
I am having the following code on my Home Controller / Viewmodel class
And following is the ActionExcuteFilter
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
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 :)
In single statement
When I try to access (without login) .URL : http://localhost:/Product?categoryId=1
getting following error.
Regards., BJ
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!
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
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)
Yes ,
It is having the document type.
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":
This seems like a simpler way to check!
More info here: https://our.umbraco.org/documentation/reference/routing/custom-controllers
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
is working on a reply...