简体   繁体   中英

call action method from global.asax

I am trying to call action method of controller from Global.asax.cs.

Here is my actionMethod:

    [HttpGet]
    public async Task<ActionResult> ErrorInsert(int Id, string severity, string description,string details)
  {
        return new EmptyResult();

  }

I have to capture the events when IIS shutdown unexpectedly in Global.asax

protected void Application_End()
        {
            var s_activityGuid = Guid.NewGuid();

                try
                {

                    System.Web.ApplicationShutdownReason shutReason = System.Web.Hosting.HostingEnvironment.ShutdownReason;
                    string shutMessage = string.Empty;
                    //var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
                    var routeData = new RouteData();
                    routeData.Values["controller"] = "ErrorRouting";
                    routeData.Values["action"] = "ErrorInsert";

                    if (shutReason == System.Web.ApplicationShutdownReason.BinDirChangeOrDirectoryRename)
                    {
                        shutMessage = "There is a shut down because of change to the Bin folder or files contained in it.";
                        routeData.Values["Id"] = 1;
                        routeData.Values["severity"] = "Error";
                        routeData.Values["description"] = shutReason;
                        routeData.Values["details"] = shutMessage;
                        IController routeController = new ErrorRoutingController();
                        var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
                        diagnosticController.Execute(rc);
                        //Response.Redirect(urlHelper.Action("ErrorInsert", "ErrorRouting",new { Id=1200, severity= "Error", description= shutReason, details= shutMessage } ));
                    }

                    log.WriteEntry(shutMessage, EventLogEntryType.Error);

                }
                catch (Exception ex)
                {

                }
            }
        }

I have tried with Response.Redirect and controller.excute() approaches.

In both cases, I am getting HttpContext as Null and it is failing.

Requirement is it should call the action method mentioned and continue the execution in Application_End() method.

Please let me know how can I do this.

I am not sure why HttpContext is coming null?

Application_End is application level event it will not execute based on the per Request basis. That is the reason you are getting HttpContext as null because its not tied to request context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM