c# - MVC Azure 中 OnActionExecuting 中 Controller 的实例化(...不抛出 404)

标签 c# asp.net-mvc azure routes onactionexecuting

目标是在 Azure MVC 站点管理的同一 URL 上添加维护批处理。网址应该类似于:

https://admin.mysite.com/Batch?pass=HKE671

我决定重写 OnActionExecuting 并捕获 URL 中所需的信息以触发 maintenance 方法。我对 MVC 项目不熟悉,这听起来可能不太传统......

  protected override void OnActionExecuting(ActionExecutingContext filterContext)
  {
      EmailUtility.SendSupportEmail("DEBUG - On result executing", ""); //I receive this email
      int y = 14;
      if (Request.Url.Query.Length > y)
      {
          string url = Request.Url.Query.Remove(0, y); // I remove ?ReturnUrl=%2f
          if (url.StartsWith("Batch"))
          {
              mySite.Admin.Web.Controllers.TenantController controller = new mySite.Admin.Web.Controllers.TenantController();
              EmailUtility.SendSupportEmail("DEBUG - starts maintenance", ""); // I don't receive this one
              controller.maintenance(HttpUtility.ParseQueryString(url).Get("pass"));
          };
      }
      base.OnActionExecuting(filterContext);
  }

这段代码在本地运行得很好,调用了 maintenance 方法并完成了工作。但是当部署在 Azure 上时,此修改会引发 404 错误。有趣的是,我发送了两封调试电子邮件:我没有收到第二封“DEBUG - 开始维护”,因此我的 404 错误来自于 OnActionExecuting 中 Controller 的实例化.

首先,我想了解为什么我的开发计算机和 Azure 之间的行为不同?

那么,我怎样才能让它发挥作用呢?谢谢,

1 月 4 日编辑:
我取得了一些进展,但这个问题仍然没有解决。
- 关于我的开发机器和 Azure 之间的区别:此站点上有一些重定向:https、404 和不存在的域。我认为这是由于 404 错误造成的。用 try/catch 封装代码并没有给我带来任何错误,所以我猜测我可以从假设中抑制 404。
- 我在 OnAuthorization 上尝试了上面的代码,但没有取得更多成功。
- 我注意到第一封电子邮件DEBUG - On resultexecuting实际上仅在第一次测试时发送。我第二次运行测试时不会发送它。这对我来说没有任何意义,因为每次都应该检查 session 。

今天的结论:这似乎更多是一个路由/重定向问题。

最佳答案

你为什么不这样做:

      protected override void OnActionExecuting(ActionExecutingContext filterContext)
      {    
         try {
            //your code here
         } catch (Exception ex){
            EmailUtility.SendSupportEmail("Execution failed: " + ex.Message , "");
         }
      }

如果您的代码抛出异常,这应该可以让您更好地理解它失败的原因。

但更重要的是,检查 URL 是否正确的更可靠方法是:

if (filterContext.HttpContext.Request.Url.AbsolutePath.EndsWith("Batch")){
}

据我所知,您的字符串变量“url”永远不会以“Batch”开头,因为Request.Url.Query仅包含“?”之后的部分从而使你的支票总是返回 false。为什么这在本地主机上工作很难说,但在我看来它不应该。

实际上,要在所有情况下工作,您的检查应该是:

if (filterContext.HttpContext.Request.Url.AbsolutePath.ToLower().EndsWith("batch") || filterContext.HttpContext.Request.Url.AbsolutePath.ToLower().EndsWith("batch/")){
}

编辑后: 好的,所发生的情况是,您在 Home Controller 中请求“Batch”操作,但由于您未经过身份验证,您将被重定向到 Account Controller 中的 LogOn。您应该看到一个登录页面,除非您的 Views 文件夹中没有关联的 LogOn View ,或者根本没有 AccountController,在这两种情况下都会引发异常。

批处理方法是否应该检查身份验证?如果没有,请从 HomeController 中的此方法中删除 [Authorize] 注释,应该没问题。

不过,您仍然需要调整 if-check,因为它实际上只在 LogOn 页面上计算为 true,如果您实际到达 Batch 页面,则计算结果为 false。

关于c# - MVC Azure 中 OnActionExecuting 中 Controller 的实例化(...不抛出 404),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35122771/

相关文章:

c# - 逻辑运算符 : (A>=100 && B<100 || B<A)

c# - 如何在 .NET 中循环并测试已实例化对象的属性值

azure - 服务主体创建另一个服务主体的最小权限

azure - 无法删除损坏的 B2C 应用程序和租户

asp.net-mvc - httpcontext 当前服务器映射路径显示 c :\inetpub\wwwroot when the current location is somewhere else

Azure webjobs队列消息可见性时间

c# - FlowLayoutPanel 自动包装不适用于自动调整大小

c# - 发生异常 .. 关键字 'Table' 附近的语法不正确

c# - 文件上的 ASP.NET 时间日期戳被覆盖

asp.net-mvc - IControllerFactory 'MvcSiteMapProvider.DI.ControllerFactoryDecorator' 未返回名称为 'Scripts' 的 Controller