Có ai biết làm thế nào để có được RequestContext hiện tại từ sự kiện Application_Error trong global.asax ?? Vấn đề của tôi là tôi cần phải làm một chuyển hướng, và do đó cần phải có url được tạo ra bằng cách sử dụng UrlHelper - trong đó có RequestContext đã đề cập ở trên.Truy cập RequestContext từ global.asax
11
A
Trả lời
11
Trong khi không có cách nào trực tiếp truy cập vào RequestContext, bạn có thể tạo một bản thân:
RequestContext context = new RequestContext(new HttpContextWrapper(HttpContext.Current), RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current)))
Vì vậy, các UrlHelper thể được xây dựng qua:
UrlHelper helper = new UrlHelper(new RequestContext(new HttpContextWrapper(HttpContext.Current), RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current))));
Không đẹp, nhưng nó được công công việc hoàn thành.
+0
Bạn có thể truy cập vào RequestContext, vui lòng tham khảo câu trả lời của tôi. –
0
Tạo một HttpContextBase từ HttpContext hiện tại, và từ đó bạn có thể tạo một UrlHelper:
// Create Http Context Base from current Context
var contextBase = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current);
// Get its request context
System.Web.Routing.RequestContext requestContext = contextBase.Request.RequestContext;
// Build url helper from request context
var urlHelper = new System.Web.Mvc.UrlHelper(requestContext);
7
Bạn có thể truy cập vào bối cảnh yêu cầu sử dụng
HttpContext.Current.Request.RequestContext
Hoặc, nếu bạn đang ở trong Global.asax
bạn có thể sử dụng
Context.Request.RequestContext
trực tiếp.
Các vấn đề liên quan
- 1. ASP.NET: Truy cập phiên biến trong global.asax
- 2. Cách truy cập thành viên tĩnh Global.asax?
- 3. Cách truy cập RequestContext trong chế độ xem chung dựa trên lớp học?
- 4. django CSRF RequestContext
- 5. Làm cách nào để truy cập các thuộc tính từ global.asax trong một số mã của trang khác sau
- 6. Truy cập iPhone từ Windows
- 7. Truy cập scala.None từ Java
- 8. Truy cập gmail từ Java
- 9. truy cập R từ SAS
- 10. truy cập localhost từ vm
- 11. Truy cập DbContext từ IQueryable
- 12. Truy cập TEAMCITY từ máy từ xa
- 13. RavenDb trên trang web Azure - Truy cập bị từ chối
- 14. Truy cập Lỗi bị từ chối truy cập vào nhà cung cấp IIS WMI từ ASP
- 15. Tại sao HttpContext.Current.Session là null trong Global.asax?
- 16. Bối rối trên global.asax?
- 17. Global.asax trong Umbraco 6
- 18. iphone - truy cập UITableViewController từ UITableViewCell
- 19. Truy cập User.Identity từ Master Page
- 20. Truy cập thuộc tính masterpage từ usercontrol
- 21. java.security.AccessControlException: Truy cập bị từ chối (java.io.FilePermission
- 22. Biến phiên truy cập từ HTTPModule
- 23. Truy cập chức năng C++ từ QML
- 24. Process.MainModule -> "Truy cập bị từ chối"
- 25. OpenCV yếu tố truy cập từ Mat
- 26. Truy cập RequestContextHolder và HttpServletRequest.getUserPrincipal() từ AuthenticationSuccessHandler
- 27. Truy cập enum từ một lớp khác
- 28. Truy cập FacesContext từ Dịch vụ Web
- 29. Truy cập Javascript TR từ TD
- 30. truy cập máy ảnh từ uiwebview?
câu trả lời này sẽ giúp bạn? http://stackoverflow.com/a/2032154/330606 –