Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 782713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:25:47+00:00 2026-05-14T20:25:47+00:00

What is the best way to test if a url & querystring is valid?

  • 0

What is the best way to test if a url & querystring is valid? For example, after a login redirect I want to make sure the target url is valid. If not, go to a default page.

We seem to have a problem with the querystring, starting with “ReturnUrl=”, being duplicated and that throws an exception. We’d rather have it go to a default page.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:25:47+00:00Added an answer on May 14, 2026 at 8:25 pm

    Here is a workaround for too long ReturnUrl querystring parameter. The fact is if there was something in the querystring before appending new RedirectUrl parameter (e.g. by using FormsAuthentication.RedirectToLoginPage method), this would be encoded and assigned to the new RedirectUrl parameter.

    The idea is to remove unnecessary(old ReturnUrl parameters from the querystring). For this I use Application_EndRequest in global.asax and Response.RedirectLocation property.

    So if the response is being redirected and current url contains ReturnUrl parameter, it should be removed from redirected location (because it doesn’t make sense).

    // parameter key
    private static readonly string ReturnUrlParameter = "ReturnUrl";
    
    protected void Application_EndRequest(object sender, EventArgs e)
    {
        if (Response.IsRequestBeingRedirected)
        {
            Uri redirectUrl;
            if (Uri.TryCreate(Response.RedirectLocation, UriKind.RelativeOrAbsolute, out redirectUrl))
            {
                redirectUrl = MakeAbsoluteUriIfNecessary(redirectUrl);
                Uri currentUrl = Request.Url;
                var currentQueryParameters = 
                        HttpUtility.ParseQueryString(HttpUtility.UrlDecode(currentUrl.Query));
                // the parameter is present in the current url already
                if (currentQueryParameters[ReturnUrlParameter] != null)
                {
                    UriBuilder builder = new UriBuilder(redirectUrl);
                    builder.Query = 
                            HttpUtility.UrlDecode(builder.Query)
                                .Replace(Request.Url.Query, string.Empty).TrimStart('?');
    
                    Response.RedirectLocation = 
                            Request.Url.MakeRelativeUri(builder.Uri).ToString();
                }
            }
        }
    }
    
    private Uri MakeAbsoluteUriIfNecessary(Uri url)
    {
        if (url.IsAbsoluteUri)
        {
            return url;
        }
        else 
        {
            Uri currentUrl = Request.Url;
            UriBuilder builder = new UriBuilder(
                    currentUrl.Scheme, 
                    currentUrl.Host, 
                    currentUrl.Port
                );
    
            return new Uri(builder.Uri, url);
        }
    }
    

    For URL parsing and building System.Uri will be the best choice.

    A URI is a compact representation of a
    resource available to your application
    on the intranet or Internet. The Uri
    class defines the properties and
    methods for handling URIs, including
    parsing, comparing, and combining. The
    Uri class properties are read-only; to
    create a modifiable object, use the
    UriBuilder class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.