Tracy Fletcher
Tracy Fletcher

Reputation: 39

ASP.Net MVC Redirect Specific Routes to External Site

I have an nicely functioning ASP.Net MVC site using the simple standard routing scheme:

routes.MapRoute(
    "Default",                                              
    "{controller}/{action}/{id}",                           
    new { controller = "Home", action = "Index", id = "" }
);

My client would like to redirect the static pages to a secondary site, so that they can edit them, template-style, at will. The pages that actually do something will remain on the original site.

What I need to do is set up routes for my functional views/controller-actions and redirect the remaining urls to the external site regardless of whether or not the specified url has a matching controller/action. I don't want to mess with the existing code, but use routing to execute some of the pages and redirect from others.

For example:

mysite.com/sponsors/signup would be executed

mysite.com/sponsors/information would be redirected

Even though the sponsors controller contains actions for both signup and information and there are existing views for both signup and information.

So far, I have been unable to wrap my head around a way to do this.

Any ideas?

Upvotes: 3

Views: 11142

Answers (4)

Nikhil Mysore
Nikhil Mysore

Reputation: 283

It is pretty simple.

Create an Action

  public ActionResult ext(string s)
        {
            return Redirect(s);
        }

And in Routeconfig file add

routes.MapRoute(name: "Default17", url: "routeyouwant", defaults: new { controller = "Home", action = "ext", s = "http://externalurl.com" });

Upvotes: 0

boateng
boateng

Reputation: 898

Step 1: add this to RouteConfig.cs

routes.IgnoreRoute("yourwebpage.aspx");

Step 2: create new file at root of website called "yourwebpage.aspx"

Step3: Inside yourwebpage.aspx put:

    <%
    Response.RedirectPermanent("http://yourwebsite.com");
     %>

Upvotes: 0

Yogiraj
Yogiraj

Reputation: 1982

You can use attribute routing to make it easier.

Your RouteConfig will look like below:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes(); // enable attribute routing

        routes.MapRoute(
            "Default",                                              
           "{controller}/{action}/{id}",                           
           new { controller = "Home", action = "Index", id = "" }
      );
    }
}

Then you can add an action like below:

public class SponsorsController : Controller
{

    [Route("sponsors/information")]
    public ActionResult RedirectInformation()
    { 
        return RedirectPermanent("http://yoururl.com");
    }
}

EDIT ONE

If you don't want to use attribute routing, you are still going to need the action but your RouteConfig will look like below:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //The order is important here
        routes.MapRoute(
            name: "redirectRoute",
            url: "sponsors/information",
            defaults: new { controller = "Home", action = "RedirectToInformation"}
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );


    }
}

In routing like this, if the match is found the rest of the routes are ignored. So, you'd want to put most specific route on top and most general in the bottom

EDIT TWO (based on the comment)

You can put a simple appsettings in Web.config like below:

<appSettings>
  <add key="UseAttributeRouting" value="true" />
</appSettings>

Then in RegisterRoutes you can read it like below and make the decision.

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        if (Convert.ToBoolean(ConfigurationManager.AppSettings["UseAttributeRouting"]))
        {
            routes.MapMvcAttributeRoutes();
        }
        else
        {
            routes.MapRoute(
                name: "redirectRoute",
                url: "sponsors/information",
                defaults: new {controller = "Home", action = "RedirectToInformation"}
                );
        }

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
} 

The browser sometimes caches these redirects, so you may want to recommend clearing browser caches if you change these settings. Check this superuser post for clearing the cache for Chrome.

Upvotes: 3

pjobs
pjobs

Reputation: 1247

You have two options i) This is perfect use case to write a custom mvchandler that is instatiated by a custom IRoutehandler. You can follow this example.

http://www.eworldui.net/blog/post/2008/04/aspnet-mvc---legacy-url-routing.aspx.

ii) You can write HttpHandler for these matching paths you can redirect them to the other site.

Upvotes: 0

Related Questions