15

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;
        }
    }
}
2

6 Answers 6

24

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>
...
1
  • My system.webserver contains already <httpRuntime targetFramework="4.0"/> so I've updated it to <httpRuntime targetFramework="4.0" maxUrlLength="10999" maxQueryStringLength="2097151" />. In case I've added a new httpRuntime tag, I've got an IIS 500.19 error.
    – zinon
    Oct 3, 2020 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 ...
}
4
  • 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, 2013 at 11:04
  • @Pomster sorry I wasn't clear, check my edit. I hope it helps. Jul 11, 2013 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). Jul 11, 2013 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, 2013 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

3
  • Iv set this to large numbers and nothing has happened?
    – Pomster
    Jul 11, 2013 at 11:03
  • <httpRuntime executionTimeout="240" maxRequestLength="20480" maxUrlLength="1000000" maxQueryStringLength="1000000"/>
    – Pomster
    Jul 11, 2013 at 11:17
  • check out my edit. there is also a way to increase the maxQueryStringLength from IIS
    – sm_
    Jul 11, 2013 at 11:24
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

3
  • The error happens from the redirect to action? why would Url length have a effect?
    – Pomster
    Jul 11, 2013 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 Jul 11, 2013 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? <httpRuntime executionTimeout="240" maxRequestLength="20480" maxUrlLength="1000000" maxQueryStringLength="1000000"/>
    – Pomster
    Jul 11, 2013 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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.