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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:11:46+00:00 2026-05-27T03:11:46+00:00

I need to provide following functionality for one of the web sites. http://www.example.com/ [sponsor]

  • 0

I need to provide following functionality for one of the web sites.

http://www.example.com/[sponsor]/{controller}/{action}

Depending on the [sponsor], the web page has to be customized.

I tried combination of registering the routes with Application_Start and Session_Start but not able to get it working.

public static void RegisterRoutes(RouteCollection routes, string sponsor)
{
        if (routes[sponsor] == null)
    {
      routes.MapRoute(
     sponsor, // Route name
     sponsor + "/{controller}/{action}/{id}", // URL with parameters
     new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
     );
    }
}

Also, the default behavior without [sponsor] should also function.
Can someone please let me know if it is technically feasible to have an optional first parameter in the MVC3 URL. If yes, please share the implementation. Thank you.


Updated Code
After making the changes as suggested by Sergey Kudriavtsev, the code works when value is given.
If name is not provided then MVC does not route to the controller/action.

Note that this works only for the home controller (both and non-sponsor). For other controllers/actions, even when sponsor parameter is specified it is not routing.

Please suggest what has to be modified.

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
             "SponsorRoute",
             "{sponsor}/{controller}/{action}/{id}", // URL with parameters
             new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            "NonSponsorRoute",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional, sponsor = string.Empty }
        );
    }

Action Method

public ActionResult Index(string sponsor)
    {
    }
  • 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-27T03:11:47+00:00Added an answer on May 27, 2026 at 3:11 am

    In your case sponsor should not be treated as a constant part of URL, but as a variable part.

    In Global.asax:

    public static void RegisterRoutes(RouteCollection routes)
    {
    ...
         routes.MapRoute(
         "SponsorRoute",
         "{sponsor}/{controller}/{action}/{id}", // URL with parameters
         new { controller = "Home", action = "Index", id = UrlParameter.Optional }
         );
         routes.MapRoute(
         "NonSponsorRoute",
         "{controller}/{action}/{id}",
         new { controller = "Home", action = "Index", id = UrlParameter.Optional, sponsor=string.Empty }
         );
    
    ...
    }
    

    In your controllers, for example, HomeController.cs:

    namespace YourWebApp.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index(string sponsor)
            {
                // Here you can do any pre-processing depending on sponsor value, including redirects etc.
            }
            ...
        }
    }
    

    Note that type of this parameter will always be System.String and the name of route template component {sponsor} must exactly match the name of action parameter string sponsor in your controllers.

    UPD: Added second route for non-sponsor case.

    Please note that such setup will complicate your logic, because you might confuse different urls, for example URL

    http://www.example.com/a/b/c

    could be matched by both routes: first one will have sponsor=a, controller=b and action=c; second one will have controller=a, action=b and id=c.

    This situation can be avoided if you specify more strict requirements to URLs – for example, you may want IDs to be numerical only. Restrictions are specified in fourth parameter of routes.MapRoute() function.

    Another approach for disambiguation is specifying separate routes for all of your controllers (usually you won’t have much of them in your app) before generic route for sponsors.

    UPD:

    Most straightforward yet least maintainable way to distinguish between sponsor and non-sponsor routes is specifying controller-specific routes, like this:

    public static void RegisterRoutes(RouteCollection routes)
    {
    ...
         routes.MapRoute(
         "HomeRoute",
         "Home/{action}/{id}", // URL with parameters
         new { controller = "Home", action = "Index", id = UrlParameter.Optional, sponsor=string.Empty }
         );
         routes.MapRoute(
         "AccountRoute",
         "Account/{action}/{id}", // URL with parameters
         new { controller = "Account", action = "Index", id = UrlParameter.Optional, sponsor=string.Empty }
         );
    
         ...
    
         routes.MapRoute(
         "SponsorRoute",
         "{sponsor}/{controller}/{action}/{id}", // URL with parameters
         new { controller = "Home", action = "Index", id = UrlParameter.Optional }
         );
    
    ...
    }
    

    Note that here all controller-specific routes must be added before SponsorRoute.

    More complex yet more clean way is implementing RouteConstraints for sponsor and controller names as described in answer from @counsellorben.

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

Sidebar

Related Questions

I have Text( http://www.mywebsite.com ) in a view, in which i need to provide
I need to provide our web developers an easy and quick way to test
I have a wcf web service and I need to provide the client with
I run a local directory website (think yelp/yell.com etc) and need to provide analytical
I'm new to WPF but I need to implement following functionality: I have a
I need to provide secure wiping function in windows mobile with following details: All
I wish to develop an application in VB.NET to provide to following functionality and
I need to provide webpage (made for desktop browsers) to be usable on mobile
I need to provide some passwords, API keys and similar sensitive data in my
On the form I need to provide the validation where the validation depends on

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.