views:

493

answers:

5

Hi there!

I feel like I'm re-inventing the wheel here, but I need to find a way of taking a URL from a catchall,and redirecting that to another route.

The reason for this is because I need to do some things like adding session cookies for certain urls, and then pass them on to their relevant action.

What's the best way of implementing this?

Thanks in advance for any help!

A: 

If I understand question...

Looking at your previous question (redirect-to-controller-but-with-a-different-master-using-a-catchall-wildcard) you want to take the wildcard from:

routes.MapRoute(
    "Partner",
    "partners/{partner}/{*wildcard}",
    new { controller = "Partners", action = "PartnerRedirect" }
);

And in the Partners/PartnerRedirect action, send it to the right controller/action specified in the wildcard?

I have no idea the best way to do this, but looking at ways to unit test url routing brought up this:

http://weblogs.asp.net/stephenwalther/archive/2008/07/02/asp-net-mvc-tip-13-unit-test-your-custom-routes.aspx

I'm not sure if it is an exact fit (ie. might not have to mock stuff), but it looks like all the data is returned from GetRouteData to pass to RedirectToAction method.

eyston
+1  A: 

Hey!

Thanks for the response! Most of the solutions I've read involve mocking another request using a faked HttpContext, but I can't help the feeling that this is a possible failing in .NET MVC.

I should be able to manually create a URL myself, and have the routing framework determine the appropriate controller and action to go to, without going to the trouble of using mocking in a production environment, although you yourself point out that this may not be the best fit.

I'm also a little concerned that, if I use RedirectToAction, the URL in the user's address bar will change, eg, using the example from my previous question:

  • User types mysite.com/partners/products/cola.htm into their address bar.
  • Website goes to the partner controller (as per the route rules), where a session cookie is set, the appropriate controller and action is determined, and a RedirectToRoute is done.
  • Their address bar changes to mysite.com/products/cola.htm (session cookie is in place, but the url has changed, potentially confusing the user).

Any further information or possibilities would be much appreciated!

Dan Atkinson
A: 

Oh, so you don't want a redirect? You want the URL to stay:

mysite.com/partners/pepsi/products/cola.htm

but serve up:

mysite.com/products/cola.htm

I don't have MVC at home, but couldn't you instead define your route differently?

Instead of:

routes.MapRoute(
    "Partner",
    "partners/{partner}/{*wildcard}",
    new { controller = "Partners", action = "PartnerRedirect" }
);

do:

routes.MapRoute(
    "Partner",
    "partners/{partner}/{controller}/{action}/{other stuff you need}",
    new { /* whatever defaults you want */ }
);

The action now has the partner variable to do whatever with, and you could add route constraints to partner so only valid ones match.

(I think) you could then use an action filter on your actions/controllers to do appropriate action based on the partner paramter (it should be in httpcontext of the filter, again I think!) so that you don't have to repeat code in every action if you want to do some basic checks/actions on partner.

Here is a decent write up on a lot of ways to skin that cat:

http://weblogs.asp.net/stephenwalther/archive/2008/08/12/asp-net-mvc-tip-31-passing-data-to-master-pages-and-user-controls.aspx

eyston
A: 

No, it is effectively a redirect, but the redirect is not visible to the user (in much the same way as a Server.Transfer() is done in Web Forms.

The link you pointed at only uses one master page, whereas I have many master pages (close to 200).

I have thought of having separate route handlers for these partners, but the master page choice came into the equation.

I think I'm going to have to create a ViewExtension to overcome these problems...

Dan Atkinson
Maybe it is possible to have your master page render a user control based on the partner parameter? Doesn't sound super clean though. I haven't read about view extensions before so can't comment.
eyston
+2  A: 

You can try changing the master page at runtime. It's not the most beautiful solution though.

Theming Support

Todd Smith