简体   繁体   中英

MVC 5 how to show message when a non-user visit the website?

I'm creating a program by using ASP.NET MVC 5. This program is needed for login and inside a page is order page. How to make this page will show some string like "User is needed to login".

As my progress now, I only know to put the [Authorize] attribute

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult PlaceOrder(Order orderDetail)
        {
            String message = "";
            using (myDatabaseEntities1 myDatabase1 = new myDatabaseEntities1())
            {
                //WF
                Double PriceOfF1 = (orderDetail.A_ChickenChop_BP) * 14.9;
                Double PriceOfF2 = (orderDetail.A_ChickenChop_M) * 14.9;
                Double PriceOfF3 = (orderDetail.A_Spaghetti_AH) * 10.9;
                Double PriceOfF4 = (orderDetail.A_Spaghetti_P) * 10.9;
                Double PriceOfF5 = (orderDetail.A_Spaghetti_S) * 10.9;
                //CF
                Double PriceOfF6 = (orderDetail.A_ChickenRice_CB) * 6.9;
                Double PriceOfF7 = (orderDetail.A_ChickenRice_CW) * 6.9;
                Double PriceOfF8 = (orderDetail.A_ChickenRice_D) * 6.9;
                Double PriceOfF9 = (orderDetail.A_WantanMee_NS) * 6.9;
                Double PriceOfF10 = (orderDetail.A_WantanMee_IS) * 6.9;

                Double T_Price = orderDetail.OrderPrice;

                T_Price = PriceOfF1 + PriceOfF2 + PriceOfF3 + PriceOfF4 + PriceOfF5 +
                    PriceOfF6 + PriceOfF7 + PriceOfF8 + PriceOfF9 + PriceOfF10;

                if (T_Price > 1)
                {
                    myDatabase1.Orders.Add(orderDetail);
                    myDatabase1.SaveChanges();
                    message = "The order has been placed";
                    orderDetail.IsPlaced = true;
                }
                else
                {
                    message = "Please select at least one of the food";
                    orderDetail.IsPlaced = true;
                }
            }
            ViewBag.Message = message;
            return View();
        }
    }

When a non-user click this page, it will redirect to the userlogin page. How to make it a message will show when the non-user clicked

This is one of the solutions. on the page you want to show the message, add the following code on the exact location you want the message to appear

  @if(!HttpContext.Current.User.Identity.IsAuthenticated)
       {
  <span class="your css class name">Please 
  <a href="@Url.Action("login action name")"> Login </a> to continue
    }

There is few ways to do it. You check inside this method if user is logged in (you have this information in request) and redirect him to page you want. It can be login page with return url to page he was before or maybe same page with information to log in - depends what you need. Just type in google "mvc5 how to check if user is logged in". Other way is using attribute authorize or adding some custom filter.

With the Above code you can use [Authorize] filter. In the Authorize filter you can validate the user is already logged in or logged in session exist or not.

If I understand your question correctly, all you need is to pop up a dialog or modal when a "un-authenticated" user clicks on "Place Order", Right ?. If that is case , Don't redirect the user to your "PlaceOrder" post method, rather create a GET method which returns a JSON status(true or false) to check whether an authenticated user session is live or not ? - Its that simple.

Or if you need some more elegant, create a custom [Authorize] extending the "AuthorizationFilterAttribute" from where you can control the view returned or check with HTTP status returned

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM