How to redirect beta/* to /*?
I have site in private beta, and now I need to redirect from beta/* to /*. How can be this done in ASP.NET MVC?
Thanks 开发者_开发问答* = everything
One way is to setup a wildcard route that will take any route with beta and send it to an action that will then redirect you to the correct url:
routes.MapRoute(
"Beta", // Route name
"beta/{*url}", // URL with parameters
new { controller = "Beta", action = "Index", url= UrlParameter.Optional }
);
Then in the Beta controller do the redirect:
public ActionResult Index(string url)
{
return Redirect("/" + url);
}
It's probably better to have the rewriting handled by IIS for you. The application shouldn't be "aware" if it is in beta mode or not.
You could do this by defining a new route and using an IRouteHandler
to handle the redirect.
See Phil Haack's blog about this: http://haacked.com/archive/2008/12/15/redirect-routes-and-other-fun-with-routing-and-lambdas.aspx
精彩评论