![](/img/trans.png)
[英]Step into: Stepping over non-user code : What is the meaning of this message?
[英]MVC 5 how to show message when a non-user visit the website?
我正在使用ASP.NET MVC 5創建程序。登錄時需要該程序,並且頁面內是訂購頁面。 如何制作此頁面將顯示一些字符串,例如“需要用戶登錄”。
就我現在的進度而言,我只知道放入[Authorize]屬性
[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();
}
}
當非用戶單擊此頁面時,它將重定向到userlogin頁面。 當非用戶點擊時,如何顯示一條消息
這是解決方案之一。 在您要顯示消息的頁面上,在您希望消息顯示的確切位置上添加以下代碼
@if(!HttpContext.Current.User.Identity.IsAuthenticated)
{
<span class="your css class name">Please
<a href="@Url.Action("login action name")"> Login </a> to continue
}
沒有什么方法可以做到的。 您可以在此方法內部檢查用戶是否已登錄(在請求中有此信息),然后將其重定向到所需的頁面。 它可以是登錄頁面,返回URL可以返回到他之前的頁面,也可以是包含登錄信息的頁面-取決於您的需求。 只需輸入google“ mvc5如何檢查用戶是否已登錄”即可。 另一種方法是使用屬性授權或添加一些自定義過濾器。
通過上述代碼,您可以使用[授權]過濾器。 在“授權”過濾器中,您可以驗證用戶是否已經登錄或登錄的會話是否存在。
如果我正確理解了您的問題,那么您需要做的就是在“未經身份驗證”的用戶單擊“下訂單”時彈出對話框或模式,對嗎? 如果是這種情況,請不要將用戶重定向到您的“ PlaceOrder”發布方法,而是創建一個返回JSON狀態(真或假)的GET方法,以檢查通過身份驗證的用戶會話是否有效? -就這么簡單。
或者,如果需要更優雅的方法,請創建一個自定義[Authorize],擴展“ AuthorizationFilterAttribute”,從中可以控制返回的視圖或檢查返回的HTTP狀態
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.