---
title: "Redirecting when the querystring is empty"  
description: "Redirecting when the querystring is empty"  
author: "Anonymous User"  
published: 2015-06-01  
updated: 2015-06-01  
canonical: https://www.mindstick.com/forum/23274/redirecting-when-the-querystring-is-empty  
category: ".net"  
tags: ["c#", "asp.net"]  
reading_time: 2 minutes  

---

# Redirecting when the querystring is empty

In my [sample](https://www.mindstick.com/news/2174/mars-mission-by-nasa-prepares-for-tests) I have two [controllers](https://www.mindstick.com/forum/155773/how-asynchronous-controllers-work-in-asp-dot-net-mvc) for a [bus service](https://answers.mindstick.com/qa/32038/which-state-is-the-first-state-to-run-electric-bus-service) the bus routes and bus [route](https://www.mindstick.com/interview/396/what-is-routed-event-in-wpf) stops. if a route hasn't been selected I need to [redirect](https://www.mindstick.com/forum/2005/radio-button-redirect) back to the bus route [index](https://www.mindstick.com/blog/198/index-in-sql-server). I

When I go directly to the bus route stop [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) it should throw me back to the bus route list but it doesn't.

```
public ActionResult Index()        {            string busRouteCode = "";            //checks query string to see if empty            if (Request.QueryString == null)            {                //checks the cookies to see if empty                if (Request.Cookies["busRouteCode"] == null)                    {                     //if empty returns to bus route controller.                     return View("index", "snBusRoutes");                    }                else                    {                        busRouteCode = Response.Cookies["busRouteCode"].Value;                        }                                         }            else             {              busRouteCode = Request.QueryString["busRouteCode"];              }            var routeStops = db.routeStops.Include(r => r.busRoute).Include(r => r.busStop);            return View(routeStops.ToList());        }
```

## Replies

### Reply by Anonymous User

I don't think Request.QueryString in its entirety is ever going to be null. Though a specific value may be null or empty. Something like this:

```
if (string.IsNullOrWhiteSpace(Request.QueryString["busRouteCode"]))    if (string.IsNullOrWhiteSpace(Request.Cookies["busRouteCode"]))        return View("index", "snBusRoutes");
```

Update: Semantically it's probably better to perform a redirect than to return the view in this case. The user is making a specific request, but that request directs them somewhere else. They should know they're going somewhere else. Something like this:

```
if (string.IsNullOrWhiteSpace(Request.QueryString["busRouteCode"]))    if (string.IsNullOrWhiteSpace(Request.Cookies["busRouteCode"]))        return RedirectToAction("index", "controllerName");
```


---

Original Source: https://www.mindstick.com/forum/23274/redirecting-when-the-querystring-is-empty

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
