OnActionExecuting标准asp.NET相同呢? [英] OnActionExecuting equivalent in standard asp.NET?
问题描述
是否有MVC.NET的 OnActionExecuting
标准asp.NET等效? ?
我认为这将是的Page_Load
,因为 OnActionExecuting
每次执行一个动作时间(或页面将被称为负载)。但我遇到的问题继承时,我尝试使用的Page_Load来代替。
因为它是非常困难的,使我的解决办法工作了的Page_Load
我想我可能没有最好的解决方案...
他们是否是等价的或足够接近有什么想法?
背景:
我转换了一块的应用MVC3成标准.NET在SharePoint Web部件包。
这里的MVC code我试着翻译,你可以看到它的用户安全位我翻译:
保护覆盖无效OnActionExecuting(ActionExecutingContext filterContext){ 如果(!SiteCacheProvider.ItemCached(enmCacheKey.SiteSetting)){ 如果(filterContext.IsImplementedGeneralPrincipal()){
IUserProfile USERPROFILE =((IGeneralPrincipal)filterContext.HttpContext.User).UserProfile; SiteCacheProvider.ChangeSiteSetting(userProfile.SiteID);
}
} base.OnActionExecuting(filterContext);
}
首先,采取帐户的没有的行动是在ASP.NET,因为模型是不同的(基于事件的) - 有'再没有方法(行为),你可以用装饰行动过滤器,这是所有关于网页周期的事件。
二,在ASP.NET中,您可以使用 HTTP模块( 的HttpApplication .BeginRequest
特别),以便通过添加所需的逻辑,以拦截来袭请求您的应用程序页面。
从MSDN:
HTTP模块使用拦截HTTP请求修改或利用
根据如身份验证需要基于HTTP的请求,
授权,会话/状态管理,日志记录,修改响应,
URL重写,错误处理,缓存....
块引用>例如:
使用系统;
使用的System.Web;
System.Collections中使用;公共类HelloWorldModule:IHttpModule的
{
公共字符串模块名
{
{返回HelloWorldModule }
} 公共无效初始化(HttpApplication的应用程序)
{
application.BeginRequest + =(新的EventHandler(this.Application_BeginRequest));
application.EndRequest + =(新的EventHandler(this.Application_EndRequest)); } 私人无效的Application_BeginRequest(对象源,EventArgs的发送)
{
HttpApplication的应用程序=(HttpApplication的)来源;
HttpContext的背景下= application.Context;
context.Response.Write(< H1> HelloWorldModule:请求&LT的开始; / H1><小时>);
}
私人无效Application_EndRequest(对象源,EventArgs的发送)
{
HttpApplication的应用程序=(HttpApplication的)来源;
HttpContext的背景下= application.Context;
context.Response.Write(<小时>< H1> HelloWorldModule:请求&LT结束; / H1>中);
}
公共无效的Dispose()
{
}
}Is there an equivalent for MVC.NET's
OnActionExecuting
in standard asp.NET? ?I thought it would be
Page_Load
sinceOnActionExecuting
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.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?
Background:
I'm converting a piece of an MVC3 application into a standard .NET to wrap in a SharePoint Web Part.
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); }
解决方案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.
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.From MSDN:
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....
For example:
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相同呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!