2015-04-17 86 views
1

我用简单的标准路由方案的功能很好的MVC ASP.Net网站:ASP.Net MVC重定向特定路由到外部网站

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

我的客户想在静态页面重定向到一个二级站点,以便他们可以随意编辑模板样式。实际上做些什么的页面将保留在原始网站上。

我需要做的是建立路线我的功能视图/控制器动作和不管指定的地址中是否有匹配的控制器/动作重定向的其他网址的外部网站是什么。我不想混淆现有的代码,但使用路由来执行一些页面并从其他页面重定向。

例如:

mysite.com/sponsors/signup会被处决

mysite.com/sponsors/information将被重定向

即使赞助商控制器包含行动两个注册和信息以及现有的注册和信息观点。

到目前为止,我已经无法换我的头周围的方式来做到这一点。

任何想法?

回答

2

您可以使用attribute routing更容易。

你RouteConfig看起来象下面这样:

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 = "" } 
    ); 
    } 
} 

然后你就可以添加动作象下面这样:

public class SponsorsController : Controller 
{ 

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

编辑一个

如果你不希望使用属性的路由,你仍然会需要的操作,但你RouteConfig看起来象下面这样:

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 } 
     ); 


    } 
} 

路由就是这样,如果找到匹配的路由的其余部分被忽略。所以,你想要把顶部和最普遍最具体的路线在底部

编辑两个(基于评论)

你可以把一个简单的AppSettings在Web.config中象下面这样:

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

然后在RegisterRoutes你可以像下面阅读并做出决定。

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 } 
     ); 
    } 
} 

浏览器有时缓存这些重定向,所以你可能要建议您清除浏览器缓存,如果你更改这些设置。请检查此superuser post以清除Chrome的缓存。

+0

非常感谢您!属性路由可能是我最好的选择。我在文章中没有提到的一件事是,我希望能够通过web.config应用程序设置关闭重定向功能。如果我使用属性路由,我可以通过有条件地包含路由来关闭它.MapMvcAttributeRoutes();在我的代码路径?这个网站运行一个孤儿院赞助计划,我所有的时间都被捐赠。感谢您给予您的时间和专业知识! –

+0

@TracyFletcher欢迎您! :)这真是太棒了,你正在捐出时间,我很乐意帮忙。请检查我的编辑。 – Yogiraj

+0

谢谢,我很欣赏客气的话,以及帮助。是的,你的编辑正是我所想的。最后一个问题是,我可以在单个控制器操作上使用多个属性吗? –

0

你有两个选择 i)这是一个完美的用例来写一个自定义的mvchandler,由自定义IRoutehandler instatiated。你可以按照这个例子。

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

ii)您可以为这些匹配路径编写HttpHandler,您可以将它们重定向到其他站点。

+0

非常感谢您的帮助!我使用现有的控制器和操作代码来捕获完全有效的mvc路径。我正确地说,如果我的自定义mvc处理程序出现在global.asax中的注册路由列表中,然后重定向到一个外部url,我应该很好走吗? –

+0

是的。你说对了。 – pjobs

+0

谢谢!重定向工作正常。虽然有一个奇怪的副作用。我的动作链接解析不正确。我的Global.asax.cs有许多条目,如:routes.Add(“ContactUs”,新的HardRedirect( “ContactUs”, strRedirectURL, new HardRedirectHandler())); –

0

步骤1:将其添加到RouteConfig.cs

routes.IgnoreRoute( “yourwebpage.aspx”);

第2步:在创建网站的根被称为 “yourwebpage.aspx”

第三步新的文件:内部yourwebpage.aspx放:

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

这是很简单的。

创建操作

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

而且在Routeconfig文件中添加

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