URI redirection cannot contain newline characters. (Query String)

I got this error The redirect URI cannot contain newlines. when running this code below. I work MVC 4 . its my working code.

  protected void Application_Error(Object sender, System.EventArgs e) { System.Web.HttpContext context = HttpContext.Current; System.Exception exception = Context.Server.GetLastError(); var stackTraceExcep = new StackTrace(exception, true); // create the stack trace var stackTrace = stackTraceExcep.GetFrames() // get the frames .Select(frame => new { // get the info FileName = frame.GetFileName(), LineNumber = frame.GetFileLineNumber(), ColumnNumber = frame.GetFileColumnNumber(), Method = frame.GetMethod(), Class = frame.GetMethod().DeclaringType, }).FirstOrDefault(); string FileName = stackTrace.FileName; string LineNumber = stackTrace.LineNumber; string ColumnNumber = stackTrace.ColumnNumber; string MethodName = stackTrace.Method.Name; string ClassName = stackTrace.Class.Name; if (!string.IsNullOrEmpty(exception.Message)) { Response.Redirect(String.Format("~/Error/{0}/?errorMessage={1},FileName={2},LineNumber={3},ColumnNumber={4},MethodName={5},ClassName={6}", "App_Error", exception.Message, FileName, LineNumber, ColumnNumber, MethodName, ClassName));//I got this error in this line } context.Server.ClearError(); } 

What is the problem? How can we solve this? I already searched and tried Google and got answers to the stack overflow. but nothing helps me.

Now what am I doing?

My Controller Action Method

 [HttpGet] public ActionResult App_Error(string errorMessage, string fileName, string lineNumber, string columnNumber, string methodName, string className) { string errorNumber = Convert.ToString(errorRepository.AddError(errorMessage, fileName, Convert.ToInt32(lineNumber), Convert.ToInt32(columnNumber), methodName, className)); ViewData["message"] = "An error has occurred in the application . The error number is : " + errorNumber; return View(); } 
+4
source share
4 answers

Try this code

I think. your file name is null or the URL field. Therefore, he got confused and displayed an error. Now I just changed my code, see Bold lines.

Edit :

And your query string format is incorrect. Please Change ,( comma) to & (and)

  protected void Application_Error(Object sender, System.EventArgs e) { System.Web.HttpContext context = HttpContext.Current; System.Exception exception = Context.Server.GetLastError(); var stackTraceExcep = new StackTrace(exception, true); var stackTrace = stackTraceExcep.GetFrames() .Select(frame => new { // get the info FileName = frame.GetFileName(), LineNumber = frame.GetFileLineNumber(), ColumnNumber = frame.GetFileColumnNumber(), Method = frame.GetMethod(), Class = frame.GetMethod().DeclaringType, }).FirstOrDefault(); **string FileName = !string.IsNullOrEmpty(stackTrace.FileName) ? Server.UrlEncode(stackTrace.FileName) : "";** string LineNumber = stackTrace.LineNumber.ToString(); string ColumnNumber = stackTrace.ColumnNumber.ToString(); string MethodName = stackTrace.Method.Name; string ClassName = stackTrace.Class.Name; if (!string.IsNullOrEmpty(exception.Message)) { Response.Redirect(String.Format("~/Error/{0}/?errorMessage={1}&fileName={2}&lineNumber={3}&columnNumber={4}&methodName={5}&className={6}", "App_Error", exception.Message, FileName, LineNumber, ColumnNumber, MethodName, ClassName)); } context.Server.ClearError(); } 
0
source

You need URLEncode your exception message using HttpUtility or UrlHelper

 HttpUtility.UrlEncode(exception.Message) 

Change the redirect line as follows

 Response.Redirect(String.Format("~/Error/{0}/?errorMessage={1},FileName={2},LineNumber={3},ColumnNumber={4},MethodName={5},ClassName={6}", "App_Error", HttpUtility.UrlEncode(exception.Message), FileName, LineNumber, ColumnNumber, MethodName, ClassName)); 
+6
source

The message is clear: you have a new line in the query line. Place a breakpoint and take a quick look at the outgoing line to find where the new line is and fix it.

Otherwise, this code may solve the problem:

 String.Format("~/Error/{0}/?errorMessage={1},FileName={2},LineNumber={3},ColumnNumber={4},MethodName={5},ClassName={6}", "App_Error", exception.Message, FileName, LineNumber, ColumnNumber, MethodName, ClassName).Replace(@"\r\n", string.Empty); 
0
source

The error message may contain not only newline characters, but also other formatting. You must encode it before sending as a query string parameter:

 UrlHelper.Encode(exception.Message) 
0
source

Source: https://habr.com/ru/post/1498367/


All Articles