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

  • SEARCH
  • Home
  • 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 3501198
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:49:45+00:00 2026-05-18T12:49:45+00:00

I’m using .NET MVC 2 If an incoming request contains the URL: http://something.com/1234 where

  • 0

I’m using .NET MVC 2
If an incoming request contains the URL:

http://something.com/1234

where 1234 is an {id} parameter. I want to be able to use the id to get some data from a database and then change the URL so that it goes to a valid controller and action.

The new URL should look something like:

http://something.com/area/username/controller/action/id

where the original id (1234) is looked up in the database and the data would translate to a specific {username}/{controller}/{action}/{id}.

I have the following routes set up in an AreaRegistration class:

context.MapRoute(
    "route1",
    "area/{controller}/{action}/{id}",
    new { action = "Index", controller = "Home" },
    new string[] { "MyApp.Areas.Controllers" }
        );

context.MapRoute(
    "route2",
    "area/{controller}/{id}",
    new { action = "Index", controller = "Home" },
    new string[] { "MyApp.Areas.Controllers" }
);

What I can’t seem to figure out is how/where to lookup the database data and change/rewrite the URL. I have tried implementing a custom RouteHandler and RouteBase but neither seem to do what I need.

This is my first SO post so forgive me if my question isn’t clear. Any suggestions are appreciated.

  • 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-18T12:49:45+00:00Added an answer on May 18, 2026 at 12:49 pm

    You need to return a RedirectToAction() to perform the URL rewrite…

    return this.RedirectToAction(action, controller);
    

    There are a whole host of overloads for specifying ids, route values, etc…

    As to looking it up in the database, that will depend on your data access model. Assuming Entity Framework or Linq it would be something like:

    DataClasses1DataContext dc = new DataClasses1DataContext();
    
    var record = from a in dc.GetTable<Order>() select id, username;
    

    To clarify how MVC works…

    I takes a URL like http://example.com/controller/action/id or http://example.com/area/controller/action/id

    and calls the appropriate action method on the specified controller. Normally you would return a view however, you can send a lot of special data types back for different results eg JSON Data, HTTP Redirects, etc.

    If an area/controller/action is omitted in the URL, the defaults from the route are used.

    So…

    If you just want to show the appropriate page, you can leave the URL as http://example.com/1234 as long as the default action/controller has the code to display the appropriate view.

    If you want to change the URL for aesthetic reasons, you would have the default controller/action take in an Id and return a RedirectToAction which points at the Controller/Action for the URL you want.

    It’s worth noting that it will generate the most minimal URL possible so if you were to have 2 actions on your default controller:

    HomeController -> Index(int id)
    HomeController -> ShowDetails(int id)
    

    the URL for index would be something like

    http://example.com/1234
    

    redirecting to ShowDetails would give a URL like this:

    http://example.com/ShowDetails/1234
    

    If ShowDetails were on a different (non-default) controller you’d get something like this:

    http://example.com/OtherController/1234
    

    That’s assuming the routes follow the standard /Controller/Action/Id format. Needless to say by registering different routes, it swaps the parameters around as appropriate.

    Hope that helps?

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but
In order to apply a triggered animation to all ToolTip s in my app,
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.