MANIMARAN RAMASAMY

MANIMARAN RAMASAMY

  • 1.8k
  • 66
  • 498

Pass string from Global.asax to Controller

Apr 18 2019 9:51 PM
Hi,
 
I am trying to pass string from Global.asax Application_BeginRequest to MVC Controller page.
 
While passing string, checking the form input and validating data in Global.asax
 
Application_BeginRequest method. If any illegal charactors found, I have to redirect to the same page with error message what they entered wrong. I don't want to display the error message in URL.
 
So, I tried through HttpContext.Current.Items["Key"]="test message" and session too.
 
Please help me, whats wrong with code and how to pass a string value to controller from Global.asax?
 
Thanks in Advance.
 
Global.asax Code:
  1. protected void Application_BeginRequest()  
  2. {  
  3.    if (Request.HttpMethod.Equals("post", StringComparison.InvariantCultureIgnoreCase))  
  4.    {  
  5.       var keys = Request.Form.AllKeys;  
  6.       foreach (var obj2 in keys)  
  7.       {  
  8.          var a = Request.Form.Get(obj2);  
  9.          string illChar = UnSafeChar(a);  
  10.          if(!string.IsNullOrEmpty(illChar))  
  11.          {  
  12.             HttpContextBase currentContext = new HttpContextWrapper(HttpContext.Current);  
  13.             UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);  
  14.             RouteData routeData = urlHelper.RouteCollection.GetRouteData(currentContext);  
  15.             string ctlr = routeData.Values["controller"as string;  
  16.             string act = routeData.Values["action"as string;  
  17.             //The below line sending null to controller   
  18.             //HttpContext.Current.Items["AppErr"] = "You have entered illegal characters : " + illChar.ToString();  
  19.             //The below session also not working   
  20.             //HttpContext.Current.Session["AppErrSession"] = "This is test message session";  
  21.              var tempUrlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);  
  22.             Response.Redirect(tempUrlHelper.Action(act, ctlr));  
  23.             }  
  24.          }  
  25.     }  
  26. }  

Answers (3)