9

(This is a more narrow question)

In my asp.net MVC action, I am looking if the ReturnUrl value is in the URL.

My Url looks like this:

http://localhost:56112/user/login?ReturnUrl=/user/settings

In my action, I am looking if that querystring value exists, and it is returning NULL?? How can this be?

The code:

if(Request.QueryString["ReturnUrl"] != null)
{

}

Tracing through the application, it is just skipping the if statement's body i.e. it is NULL.

How can this be explained?

Update

In the controller that checks if the user has logged in, I have a ActionFilter that looks like:

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
        // some stuff
        string loginUrl = FormsAuthentication.LoginUrl + "/user/settings;

         context.Response.Redirect(loginUrl);
    }
HasanG
  • 12,734
  • 29
  • 100
  • 154
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • I just ran a quick test, and it worked fine for me. Are you using any custom http handlers or the like? – Jace Rhea Jan 29 '10 at 15:46
  • Take a look at this as well: http://stackoverflow.com/questions/1783059/query-string-with-slashes-not-working-in-asp-net-mvc – Jace Rhea Jan 29 '10 at 15:52
  • @jacerhea I have a custom filter which does the redirect to the login page. see my update above – Blankman Jan 29 '10 at 19:44
  • Use something like FireBug to check what's the real request that your application get; maybe it's the browser that displays one URL and uses another? – queen3 Jan 29 '10 at 21:45
  • 1
    You don't need `!= null` in your code. if the Request.QueryString[] is null it will return 0 i.e FALSE . – Conrad C Sep 27 '12 at 18:18

4 Answers4

3

Try debugging the code - you should be able to see in the debugger the entire list of QueryString parameters, so you can see if you miss-spelt it.

Justin
  • 84,773
  • 49
  • 224
  • 367
2

Just ran into a similar issue myself.

Request.RawUrl is correct, the url submitted to the browser is missing the QueryString. Check the View Source for the page, and verify the url on the action attribute of your form element. You'll likely find the QueryString is missing.

What's probably happening is that you're using an overload of @Html.BeginForm that takes the routeValues parameter. Using any overload with "routeValues" will generate a new routing URL and strip any querystring parameters. In my case, I wanted to add the enctype attribute and the only overload requires routeValues to be specified. I suppose it's an oversight in MVC 3.

However, you can still write your own form tag the old-fashioned way. Looking at the source code for MVC 3, it appears that the base overload for Html.BeginForm uses Request.RawUrl.

Try the following:

<form action="@Request.RawUrl" method="post" enctype="multipart/form-data">
ShadowChaser
  • 5,520
  • 2
  • 31
  • 33
1

I encountered this problem myself and had some difficulty identifying the most optimal solution, so I thought I'd add my brief experience to the conversation.

In debug mode, I scrolled down through all of the properties of the Request; in my case, I found the QueryString value I was seeking in HttpContext.Request.UrlReferrer.Query. The reason for this seems to be that I had clicked on an Html.ActionLink from the page that owned that QueryString value, and that pushed me to an MVC ActionResult handler -- which is technically treated as a separate page (with its own newly defined HttpContext), even though it doesn't represent as such on the URL in the browser while you're debugging.

Further research seems to suggest that Microsoft's preferred solution to this issue is to just pass your value down the chain when you call the handler... but the syntax for making that happen is perhaps not as clear as it should be. In lieu of that, parsing your value from the UrlReferrer seems to work just fine, even if it takes one or two extra lines of code to reach your value. For example, the most basic case scenario with a single value passed on the QueryString could look something like this:

String query = HttpContext.Request.UrlReferrer.Query;
String myValue = query.Split('=')[1];
zarmanto
  • 21
  • 5
0

I had a similar problem as novice Asp.Net MVC developer. The problem was a login form which was posting the form to its specific controller. So missing query string values are very normal. Because form action attribute is like action="/index/login"

The solution which I applied is to add a hidden field to my form with value from the query string. This needs to be added as parameter to the ActionResult or the model.

@Html.Hidden("returnUrl", Request.QueryString["returnUrl"])

Another solution is to pass it to the form action url like:

@using (Html.BeginForm("login", "contr", new {returnUrl = Request.QueryString["returnUrl"]})
HasanG
  • 12,734
  • 29
  • 100
  • 154