Url.Action does not use the desired route for output

I have the following route definitions on my MVC3 website:

routes.MapRoute(
                "FB", // Route name
                "fb/{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            ).RouteHandler = new RH();

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

My custom RH handler code

public class RH : MvcRouteHandler
    {
        protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            //here I store somewhere that 'fb' prefix is used, so logic is different in some places
            return base.GetHttpHandler(requestContext);
        }
    }

What I want to achieve is that when my site is accessible with the fb subfile prefix, then my web logic runs a little differently.

The problem is that when I usually access my site (e.g. http: // localhost ), then when I do

Url.Action('action' 'controller')

then the output will be "http: // localhost / fb / controller / action".

I want to achieve that when my site was accessible using the 'fb' prefixed subfile, then my Url.Action calls the output / fb / controller / action path, and if I usually access the website (without the 'fb' subfile) prepath), then Url.Action calls the output / controller / action

, /fb/controller/actions /, /controller/action format.

'fb' , fb '.

+3
1

, (MVC 3 Routing and Action Links, ), .

, , , , , :

routes.MapRoute(
        "FB", // Route name
        "{path}/{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
        new { path = "fb" }
).RouteHandler = new RH();

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
+3

All Articles