【问题标题】:Execute some code for each request for ASP.NET (aspx and cshtml )为 ASP.NET(aspx 和 cshtml)的每个请求执行一些代码
【发布时间】:2013-03-25 06:51:26
【问题描述】:

在asp.net 4.5 除了使用基本页面类之外,有没有办法编写一些代码,这些代码将针对每个对.aspx 或.cshtml 页面的请求执行。这是一个非常庞大的项目,对所有页面进行更改以使用基本页面是一场噩梦。我也不确定如何为 cshtml 页面完成此操作,因为它们没有类。

我们可以使用 Application_BeginRequest 并仅针对 aspx 和 cshtml 文件,因为网站是在集成模式下运行的。?

基本上,我必须检查正在访问该网站的用户是否具有针对数据库的特定 IP 地址,如果是,则允许访问,否则重定向。

我们正在使用 IIS8 和 ASP.Net 4.5 和 ASP.Net Razor 网页

【问题讨论】:

    标签: asp.net razor global-asax iis-8


    【解决方案1】:

    我也不确定如何为 cshtml 页面完成此操作,因为它们没有类。

    您可以放置​​一个_ViewStart.cshtml 文件,其内容将在每次请求时执行。

    或者你可以写一个custom Http Module:

    public class MyModule: IHttpModule
    {
        public void Init(HttpApplication app)
        {
            app.BeginRequest += new EventHandler(OnBeginRequest);
        }
    
        public void Dispose()
        { 
    
        }   
    
        public void OnBeginRequest(object s, EventArgs e)
        {
            // this code here's gonna get executed on each request
        }
    }
    

    然后只需在您的 web.config 中注册此模块:

    <system.webServer>
        <modules>
            <add name="MyModule" type="SomeNamespace.MyModule, SomeAssembly" />
        </modules>
        ...
    </system.webServer>
    

    或者如果您在经典模式下运行:

    <system.web>
        <httpModules>
            <add name="MyModule" type="SomeNamespace.MyModule, SomeAssembly" />
        </httpModules>
    </system.web>
    

    基本上,我必须检查正在访问该网站的用户是否有 针对数据库的特定 IP 地址,如果是,则允许访问 否则重定向。

    OnBeginRequest 方法中你可以获得当前用户IP:

        public void OnBeginRequest(object sender, EventArgs e)
        {
            var app = sender as HttpApplication;
            var request = app.Context.Request;
            string ip = request.UserHostAddress;
            // do your checks against the database
        }
    

    【讨论】:

      【解决方案2】:

      Asp.net MVC 过滤器专为此目的而设计。

      你会像这样实现 ActionFilterAttribute(也许把这个新类放在你的 webapp 解决方案的 Filters 文件夹中):

      public class IpFilter : ActionFilterAttribute
      {
          public override void OnActionExecuting(ActionExecutingContext filterContext)
          {
              string ip = filterContext.HttpContext.Request.UserHostAddress;
      
              if(!testIp(ip))
              {
                  if (true /* You want to use a route name*/)
                      filterContext.Result = new RedirectToRouteResult("badIpRouteName");
                  else /* you want an url */
                      filterContext.Result = new RedirectResult("~/badIpController/badIpAction");
              }
      
              base.OnActionExecuting(filterContext);
          }
          private bool testIp(string inputIp)
          {
              return true /* do you ip test here */;
          }
      }
      

      然后你必须像这样装饰任何会使用 IpFilter 执行 ipcheck 的操作:

          [IpFilter]
          public ActionResult AnyActionWhichNeedsGoodIp()
          {
               /* do stuff */
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-16
        • 2017-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-22
        • 1970-01-01
        相关资源
        最近更新 更多