Вопрос

I got this error Redirect URI cannot contain new-line characters. when run this below code. Am Working MVC 4 . hers is 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 Problem ? How can we solve this ? i have already searched and tried Google and get some stack overflow answers. but anything does not help me .

Now what i do ?

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();
        }
Это было полезно?

Решение 2

Try this code

I think . your filename is null , or Url field . So it's confused and display the error . Now i just changed your code , see the bold lines .

Edit :

And your Query String format is wrong . 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();
        }

Другие советы

You need to URLEncode your exception message with HttpUtility or UrlHelper

HttpUtility.UrlEncode(exception.Message)

Change your 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));

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

UrlHelper.Encode(exception.Message)

You can also use Session in (asp.net) or tempdata in MVC to do the same

The message is clear: You have a new line in your query string. Place a breakpoint and do a quick watch on the ofending line to find where is the new line 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);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top