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 550693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:18:56+00:00 2026-05-13T11:18:56+00:00

I’m looking for a best practice solution that aims to reduce the amount of

  • 0

I’m looking for a best practice solution that aims to reduce the amount of URLs that are hard-coded in an ASP.NET application.

For example, when viewing a product details screen, performing an edit on these details, and then submitting the changes, the user is redirected back to the product listing screen. Instead of coding the following:

Response.Redirect("~/products/list.aspx?category=books");

I would like to have a solution in place that allows me to do something like this:

Pages.GotoProductList("books");

where Pages is a member of the common base class.

I’m just spit-balling here, and would love to hear any other way in which anyone has managed their application redirects.

EDIT

I ended up creating the following solution: I already had a common base class, to which I added a Pages enum (thanks Mark), with each item having a System.ComponentModel.DescriptionAttribute attribute containing the page’s URL:

public enum Pages
{
    [Description("~/secure/default.aspx")]
    Landing,
    [Description("~/secure/modelling/default.aspx")]
    ModellingHome,
    [Description("~/secure/reports/default.aspx")]
    ReportsHome,
    [Description("~/error.aspx")]
    Error
}

Then I created a few overloaded methods to handle different scenarios. I used reflection to get the URL of the page through it’s Description attribute, and I pass query-string parameters as an anonymous type (also using reflection to add each property as a query-string parameter):

private string GetEnumDescription(Enum value)
{
    Type type = value.GetType();
    string name = Enum.GetName(type, value);

    if (name != null)
    {
        FieldInfo field = type.GetField(name);
        if (field != null)
        {
            DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;

            if (attr != null)
                return attr.Description;
        }
    }

    return null;
}

protected string GetPageUrl(Enums.Pages target, object variables)
{
    var sb = new StringBuilder();
    sb.Append(UrlHelper.ResolveUrl(Helper.GetEnumDescription(target)));

    if (variables != null)
    {
        sb.Append("?");
        var properties = (variables.GetType()).GetProperties();

        foreach (var property in properties)
            sb.Append(string.Format("{0}={1}&", property.Name, property.GetValue(variables, null)));
    }

    return sb.ToString();
}

protected void GotoPage(Enums.Pages target, object variables, bool useTransfer)
{
    if(useTransfer)
        HttpContext.Current.Server.Transfer(GetPageUrl(target, variables));
    else
        HttpContext.Current.Response.Redirect(GetPageUrl(target, variables));
}

A typical call would then look like so:

GotoPage(Enums.Pages.Landing, new {id = 12, category = "books"});

Comments?

  • 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-13T11:18:57+00:00Added an answer on May 13, 2026 at 11:18 am

    I’d suggest that you derive your own class (“MyPageClass”) from the Page class and include this method there:

    public class MyPageClass : Page
    {
        private const string productListPagePath = "~/products/list.aspx?category=";
        protected void GotoProductList(string category)
        {
             Response.Redirect(productListPagePath + category);
        }
    }
    

    Then, in your codebehind, make sure that your page derives from this class:

     public partial class Default : MyPageClass
     {
          ...
     }
    

    within that, you can redirect just by using:

     GotoProductList("Books");
    

    Now, this is a bit limited as is since you’ll undoubtedly have a variety of other pages like the ProductList page. You could give each one of them its own method in your page class but this is kind of grody and not smoothly extensible.

    I solve a problem kind of like this by keeping a db table with a page name/file name mapping in it (I’m calling external, dynamically added HTML files, not ASPX files so my needs are a bit different but I think the principles apply). Your call would then use either a string or, better yet, an enum to redirect:

     protected void GoToPage(PageTypeEnum pgType, string category)
     {
          //Get the enum-to-page mapping from a table or a dictionary object stored in the Application space on startup
          Response.Redirect(GetPageString(pgType) + category);  // *something* like this
     }
    

    From your page your call would be: GoToPage(enumProductList, “Books”);

    The nice thing is that the call is to a function defined in an ancestor class (no need to pass around or create manager objects) and the path is pretty obvious (intellisense will limit your ranges if you use an enum).

    Good luck!

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

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.