如果会话在mvc中到期,则从ActionFilter显示弹出窗口(Show popup from ActionFilter if session expires in mvc)
我们使用mvc创建了一个网站,其中我有3个控制器:
AccountController HomeController ContactController
我们在ActionFilters中编写了自定义授权,我们正在检查会话是否已过期。 如果会话到期,我们将重定向到登录页面。 这是ActionFilter中的代码:
public override void OnAuthorization(AuthorizationContext filterContext) { var user = filterContext.HttpContext.User; var UserProcesses = HttpContext.Current.Session["UserProcesses"] != null ? ((List<string>)HttpContext.Current.Session["UserProcesses"]) : null; var currentRequest = filterContext.ActionDescriptor.ActionName.ToLower(); var currentController = filterContext.RouteData.Values["controller"].ToString(); if (!string.IsNullOrEmpty(currentController)) { switch (currentController.ToLower()) { case "home": { if (UserProcesses == null) { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary( new { Controller = "Account", Action = "Login" }) ); } break; case "contact": { if (UserProcesses == null) { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary( new { Controller = "Account", Action = "Login" }) ); } break; } } }
这很有效,即当会话到期并且用户点击网站上的任何链接时,他被重定向到登录页面,其地址是
http://websitename/Account/Login
但现在我们想要在页面上显示弹出窗口(用户所在的任何页面),这将显示消息“该会话已过期”,并且在解除该弹出窗口时,用户将被重定向到登录页面。
我无法弄清楚如何从ActionFilter本身调用Jquery弹出窗口。 我在网上搜索了很多解决方案,但找不到正确的解决方案。 有人建议使用HandleUnauthorizedRequest来调用Action方法( 从ActionFilter调用RedirectToAction )。 但是我很困惑是否只使用HandleUnauthorizedRequest或在我的代码中使用this和OnAuthorization 。 有人可以建议一个解决方案?
注意:我有一个共同的视图,其中我可以保持html弹出窗口在整个网站上使用。
We have created a website using mvc where in I have 3 controllers:
AccountController HomeController ContactController
We have written custom Authorization in the ActionFilters wherein we are checking whether the session has expired or not. If the session expires we are redirecting to the login page. This is the code in the ActionFilter:
public override void OnAuthorization(AuthorizationContext filterContext) { var user = filterContext.HttpContext.User; var UserProcesses = HttpContext.Current.Session["UserProcesses"] != null ? ((List<string>)HttpContext.Current.Session["UserProcesses"]) : null; var currentRequest = filterContext.ActionDescriptor.ActionName.ToLower(); var currentController = filterContext.RouteData.Values["controller"].ToString(); if (!string.IsNullOrEmpty(currentController)) { switch (currentController.ToLower()) { case "home": { if (UserProcesses == null) { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary( new { Controller = "Account", Action = "Login" }) ); } break; case "contact": { if (UserProcesses == null) { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary( new { Controller = "Account", Action = "Login" }) ); } break; } } }
This works perfectly i.e. when the session expires and the user clicks on any link on the website, he is redirected to the login page the address of which is
http://websitename/Account/Login
But now we want to display a popup on the page(whatever page the user is in) which will show the message "That session has expired" and on dismissing that popup, the user is then redirected to the login page.
I am unable to figure out how to call a Jquery popup from the ActionFilter itself. I have searched through many solutions on the web but couldn't find the correct solution to this. Some one suggested to use HandleUnauthorizedRequest to call an Action method (Call RedirectToAction from ActionFilter). However I am confused whether to use only HandleUnauthorizedRequest or use both this and OnAuthorization in my code. Can someone please suggest a solution?
Note: I have a common View wherein I can keep the html for popup to be used across the website.
原文:https://stackoverflow.com/questions/32651699
最满意答案
检查设备型号:
let model = UIDevice.currentDevice().model if model == "iPad" { let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName as NSCopying) segmentedControl.setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal) }else{ let attr = NSDictionary(object: UIFont(name: "HelveticaNeue", size: 16.0)!, forKey: NSFontAttributeName as NSCopying) segmentedControl.setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal) }
代码可以更好。
Check device model:
let model = UIDevice.currentDevice().model if model == "iPad" { let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName as NSCopying) segmentedControl.setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal) }else{ let attr = NSDictionary(object: UIFont(name: "HelveticaNeue", size: 16.0)!, forKey: NSFontAttributeName as NSCopying) segmentedControl.setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal) }
The code can be better.
相关问答
更多-
您可以使用以下代码动态添加选择: // Removing previous selection [bottomBorder removeFromSuperlayer]; // Creating new layer for selection bottomBorder = [CALayer layer]; bottomBorder.borderColor = [UIColor redColor].CGColor; bottomBorder.borderWidth = 3; // C ...
-
这是一个更有效的方法: /** * Sets the text size for a Paint object so a given string of text will be a * given width. * * @param paint * the Paint to set the text size for * @param desiredWidth * the desired width * @param text * ...
-
分段控制段图像(Segmented control segment images)[2023-08-17]
如果您使用的是iOS 5或更高版本,则可以使用UIImage的resizableImageWithCapInsets:方法为分段控件创建可调整大小的按钮,然后只在其上呈现文本。 这里有一个很好的教程。 基本上,您所要做的就是创建具有不同端点的中心按钮图像的版本以及可以调整大小的中心。 If you're using iOS 5 or later, you can use UIImage's resizableImageWithCapInsets: method to create resizable but ... -
没关系,我找到答案/问题在哪里! 在heightForRowAtIndexPath方法中,我在抓取对象时传入了错误的数组 if (self.followingNews.count == 0) { return 50; } else { PFObject *eachNews = [self.myNews objectAtIndex:indexPath.row]; 这个IF语句的第一部分中的数组未用于PFObject的初始 ...
-
您可以简单地将复选框的状态存储在变量中,并根据这些变量隐藏div。 下面包含的第一个变量用于页面加载,其他用于对象更改时。 在这里查看: https : //jsfiddle.net/ezfy66f9/ var checked1 = $('#option-1').is(':checked'); var checked2 = $('#option-2').is(':checked'); var checked3 = $('#option-3').is(':checked'); if (checked1 == ...
-
你为什么要这样呢? Wpf使用抽象坐标,并使用当前的Monitor DPI和分辨率将它们转换为像素。 因此,您的应用程序应自动扩展为在每个屏幕上看起来相似。 我会建议,使用弹性布局,滚动条等。使您的应用程序在较小的屏幕上使用。 Why would you want this anyway? Wpf uses abstract coordinates, and convert them to pixels using the current Monitor DPI and resolution. So, yo ...
-
检查设备型号: let model = UIDevice.currentDevice().model if model == "iPad" { let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName as NSCopying) segmentedControl.setTitleTextAttributes(attr as [NSO ...
-
你需要两个边距,一个在右边,一个在左边,所以宽度应该是 let width = self.view.frame.size.width - self.margin*2 let segmentFrame = CGRect(x: self.margin, y: 64.0, width: width, height: 50.0) you need two margins one on the right and one on the left so the width should be let width = ...
-
假设你有一个非常基本的RichTextBox ,里面只有标准的FlowDocument ,那么你必须考虑RichTextBox使用的所有额外布局。 Document.PagePadding属性默认为{5,0,5,0} 。 所以你必须在宽度上加10。 此外, RichTextBox的BorderThickness默认为{1, 1, 1, 1} BorderThickness {1, 1, 1, 1} 。 所以你必须在宽度上加2。 所以你想要的代码看起来像: var pagePadding = rtb.Docu ...
-
rdelmar是对的。 这是一个与我的框架设置相矛盾的约束问题。 之后切换到使用自动布局和约束做所有事情。 rdelmar was right. It was an issue with constraints contradicting my frame settings. Switched to doing everything with auto layout and constraints afterwards.