问题描述
在标准 asp.NET 中,MVC.NET 的 OnActionExecuting
是否有等价物??
Is there an equivalent for MVC.NET's OnActionExecuting
in standard asp.NET? ?
我认为它会是 Page_Load
因为每次执行操作(或加载页面)时都会调用 OnActionExecuting
.但是当我尝试使用 Page_Load 时遇到了继承问题.
I thought it would be Page_Load
since OnActionExecuting
would be called each time an action is executed (or the page loads). But I'm running into inheritance issues when I try to use Page_Load instead.
由于很难使我的解决方案与 Page_Load
一起工作,我想我可能没有最好的...解决方案.
Since it is very difficult to make my solution work with a Page_Load
I'm thinking I might not have the best ... solution.
关于它们是否相等或足够接近的任何想法?
Any thoughts on whether they are equivalent or close enough?
背景:
我正在将 MVC3 应用程序的一部分转换为标准 .NET 以包装在 SharePoint Web 部件中.
I'm converting a piece of an MVC3 application into a standard .NET to wrap in a SharePoint Web Part.
这是我正在尝试翻译的 MVC 代码,您可以看到我正在翻译的用户安全位:
Here's the MVC code I'm trying to translate, as you can see its the user security bits I'm translating:
protected override void OnActionExecuting(ActionExecutingContext filterContext) {
if (!SiteCacheProvider.ItemCached(enmCacheKey.SiteSetting)) {
if (filterContext.IsImplementedGeneralPrincipal()) {
IUserProfile userProfile = ((IGeneralPrincipal)filterContext.HttpContext.User).UserProfile;
SiteCacheProvider.ChangeSiteSetting(userProfile.SiteID);
}
}
base.OnActionExecuting(filterContext);
}
推荐答案
首先,考虑到 没有 Actions 在 ASP.NET 中,因为模型不同(基于事件) - 有没有可以用 装饰的方法(操作)操作过滤器,都是关于页面循环事件.
First, take on account that no Actions are in ASP.NET because the model is different (Event-Based) - There're no methods(actions) which you can decorate with Action Filters, it's all about the Page-Cycle events.
第二,在 ASP.NET 中,您可以使用 HTTP 模块 (HttpApplication.BeginRequest
特别是)为了通过添加所需的逻辑来拦截对您的应用程序页面的传入请求.
Second, In ASP.NET, you may use HTTP modules (HttpApplication.BeginRequest
particularly) in order to intercept incoming requests to your application pages by adding your required logic.
来自 MSDN:
HTTP 模块用于拦截 HTTP 请求以进行修改或利用根据身份验证等需要基于 HTTP 的请求,授权、会话/状态管理、日志记录、修改响应、URL 重写、错误处理、缓存....
HTTP Modules use to intercept HTTP requests for modifying or utilize HTTP based requests according to needs like authentication, authorization, session/state management, logging, modifying Response, URL rewriting, Error handling, Caching....
例如:
using System;
using System.Web;
using System.Collections;
public class HelloWorldModule : IHttpModule
{
public string ModuleName
{
get { return "HelloWorldModule"; }
}
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
application.EndRequest += (new EventHandler(this.Application_EndRequest));
}
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Write("<h1>HelloWorldModule: Beginning of Request</h1><hr>");
}
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Write("<hr><h1>HelloWorldModule: End of Request</h1>");
}
public void Dispose()
{
}
}
这篇关于OnActionExecuting 在标准 asp.NET 中等效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!