views:

113

answers:

3
+1  Q: 

MVC Route Question

Hi,

I am trying to map an action with no controller to a specific action, However, I do not want to show the controller in the URL. I have partially achived this using the mapping shown below:

        routes.MapRoute(
            null,
            "Contact",
            new { controller = "Home", action = "Contact" });

This successfully maps requests made while on the home controller pages to

http://localhost:8082/Contact However, if the same request is made on a different page handled by a different controller, such as "NotHome" I get a page not found error because the route searched is NotHome/Contact.

Thanks in advance for any help or advise given..

+1  A: 

Try adding this route:

routes.MapRoute(
    "ContactsToHome",
    "{controller}/Contact",
    new { controller = "Home", action = "Contact" });

This will redirect any of your controller's Contact action to Home controller.

RaYell
Yes but I would also like to hide the controller in the url. :(
Then you need to define redirections on your server instead of routes.
RaYell
+1  A: 
routes.MapRoute(
                "Contact", 
                "{resource}/Contact",
                new {controller = "Home", action = "Contact"}
               );

Not sure is this is quite what you want to do, but this should (if added above the default route!), route all requests for Contact action regardless of controller to the Home/Contact combination of controller/actions.

To get /Contact as your URL

In Home / Contact add

HttpContext context = HttpContext.Current;
context.RewritePath("~/Contact");

Kindness,

Dan

Daniel Elliott
This does infact do what you say and redirects to the correct action however the controller is displayed in the url, home/Contact and even notHome/Contact when what I need is simply Contact
Can you not use wildcards here? Something like * or maybe regular expression to match the controller part?
rohit.arondekar
Edited to Rewrite your URL
Daniel Elliott
Dan,HttpContext context = HttpContext.Current; Current cannot be resolved? any ideas?Cheers
This works but does not rewrite the url displayed as expected:HttpContext.RewritePath("~/Contact");
+1  A: 

It turns out the solution was a simple one:

routes.MapRoute( "ContactsTOHome", "Contact", new { controller = "Home", action = "Contact" });

the route mapping was fine and needed no changes apart from the name. the above route hides the controller in the URL However my problem lies with calling the action:

"<%= Url.Action("Contact") %>">Contact Us

The above code should have included the controller as well as the action:

<%= Url.Action("Contact","Home") %>">Contact Us

Thanks for all the help and advice given.

I do have a follow up question regarding a comment dan made:

HttpContext context = HttpContext.Current; context.RewritePath("~/Contact");

the above code did not compile because HttpContext does not contain Current However it was possible to do the following: HttpContext.RewritePath("~/Contact");

My questions are why is there no current and why did the line of code I put not rewrite the url??

related questions