16

I am trying to redirect to a view and keep getting the error posted in the question title.

During breakpoint testing the code passing though the first bit of code iv lay down below setting the message and setting the exception. after continuing after the return redirect the very next page displayed is as follows.

enter image description here

Adding break points to the ErrorController and error model i found that the code never gets there.

The view i'm trying to post to is an error page. Here is so code to help you see the problem.

The RedirectToAction:

string message;
message = "An error has occured during the communication to lightstone, this is likely a timeout issue and could be the result of a bad connection. Please go back and try again.";
return RedirectToAction("Error", "Error", new { ex = ex.ToString(), message =  message});

The action in my ErrorController:

public ActionResult Error(string ex, string message)
{
   ViewBag.Message = "Error";
   return View(new ErrorModel(ex, message));
}

My Error model:

namespace MvcResComm.Models
{
    public class ErrorModel
    {
        public string ex { get; set; }
        public string message { get; set; }

        public ErrorModel(string ex, string message)
        {
            this.ex = ex;
            this.message = message;
        }
    }
}
Pomster
  • 14,567
  • 55
  • 128
  • 204
  • You might check http://stackoverflow.com/questions/8159321/request-exceeds-the-configured-maxquerystringlength-when-using-authorize for an answer. – Szymon Kuzniak Jul 11 '13 at 10:05
  • I understand the problem now, but still have no working solution to fix it. – Pomster Jul 11 '13 at 11:27

6 Answers6

25

In the root web.config for your project, under the system.web node:

<system.web>
    <httpRuntime maxUrlLength="10999" maxQueryStringLength="2097151" />
...

In addition, I had to add this under the system.webServer node or I got a security error for my long query strings:

<system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxUrl="10999" maxQueryString="9999" />
      </requestFiltering>
    </security>
...
theJerm
  • 4,482
  • 2
  • 30
  • 23
  • My `system.webserver` contains already `` so I've updated it to ``. In case I've added a new `httpRuntime` tag, I've got an `IIS 500.19` error. – zinon Oct 03 '20 at 12:51
4

Why don't you use TempData, it's meant to do stuff like this. So for example:

TempData["ErrorMessage"] = "An error has occured during the communication to lightstone, this is likely a timeout issue and could be the result of a bad connection. Please go back and try again.";

Check this link.

EDIT

Pass your exception message like this:

TempData["Error"] = ex.Message();
TempData["ErrorMessage"] = "An error has occured during the communication to lightstone, this is likely a timeout issue and could be the result of a bad connection. Please go back and try again.";

return RedirectToAction("Error", "Error");

Then just access it from your ErrorController, something like:

public ActionResult Error(string ex, string message)
{
    var error = (string)TempData["Error"];
    // do other magic ...
}
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
  • Thanks, i will make sure to do this, but i think the error is before i get tho the error controller. The error happens from the redirect to action. – Pomster Jul 11 '13 at 11:04
  • @Pomster sorry I wasn't clear, check my edit. I hope it helps. – Dimitar Dimitrov Jul 11 '13 at 11:27
  • @Pomster, Actually, I didn't test it, but I'm pretty sure you can pass it straight to the view (I mean you can access `TempData["Error"]` directly in your view, you don't have to pass it). – Dimitar Dimitrov Jul 11 '13 at 11:31
  • 1
    Great thanks it works, In the action result just remove the (string ex, string message), in the parameter section and left the code the same useing the TempDate[] to pass the large strings. Thanks alot huge help, Following the other answers i would have a huge url on my error pages, this is defiantly the better way. – Pomster Jul 11 '13 at 11:35
2

In you web.config under the <system.web> <httpRuntime> tags you can set your maxQueryStringLength

so its like

<system.web>
  <httpRuntime maxQueryStringLength = "**MY NUMBER**" />
</system.web>

check out the msdn reference : http://msdn.microsoft.com/en-us/library/e1f13641%28v=vs.100%29.aspx

Also please increase maxQueryStringLength in the IIS configuration, check out :

http://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits

DM14
  • 188
  • 3
  • 13
sm_
  • 2,572
  • 2
  • 17
  • 34
1

There is a maxumum URL length value settable in the web.config file. This question has a similar problem ASP.NET MVC, Url Routing: Maximum Path (URL) Length

Community
  • 1
  • 1
David Colwell
  • 2,450
  • 20
  • 31
  • The error happens from the redirect to action? why would Url length have a effect? – Pomster Jul 11 '13 at 11:06
  • Because the URL length is a defined limit on the size of the GET request. This is done to ensure support for earlier browsers that could not handle URL lengths over a certain size. When you redirect to an Action (on the controller) it does a GET on that action – David Colwell Jul 11 '13 at 11:09
  • Thanks ok i see now, how would i then pass these strings from the one controller where the error occurs to the Error Controller, with out having to pass it in the url? Or how do i set max url size? – Pomster Jul 11 '13 at 11:16
0

I fixed follow: It run Ok

<system.webServer>
    <security>
        <requestFiltering>
            <alwaysAllowedQueryStrings>
                <add queryString="maxQueryString" />
                <add queryString="maxAllowedContentLength" />
                <add queryString="maxUrl" />
            </alwaysAllowedQueryStrings>
            <requestLimits maxUrl="10999" maxQueryString="2097151" />
        </requestFiltering>
    </security>
</system.webServer>

And add

<system.web>
    <httpRuntime maxUrlLength="10999" maxQueryStringLength="2097151" />
</system.web>

web.config

0

this fixed my problem:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.web>
        <customErrors mode="Off"/>
        <httpRuntime maxUrlLength="10999" maxQueryStringLength="2097151"/>
    </system.web>
    <system.webServer>
        <security>
            <requestFiltering>
                <requestLimits maxUrl="10999" maxQueryString="9999"/>
            </requestFiltering>
        </security>         
    </system.webServer>
</configuration>
Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55