Asp.net mvc Net MVC将特定路由重定向到外部站点

Asp.net mvc Net MVC将特定路由重定向到外部站点,asp.net-mvc,redirect,controller,routing,Asp.net Mvc,Redirect,Controller,Routing,我有一个功能良好的ASP.Net MVC站点,使用简单的标准路由方案: routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" } ); 我的客户希望将静态页面重

我有一个功能良好的ASP.Net MVC站点,使用简单的标准路由方案:

routes.MapRoute(
    "Default",                                              
    "{controller}/{action}/{id}",                           
    new { controller = "Home", action = "Index", id = "" }
);
我的客户希望将静态页面重定向到辅助站点,以便他们可以随意编辑它们,模板样式。实际上做了一些事情的页面将保留在原始站点上

我需要做的是为我的功能视图/控制器操作设置路由,并将剩余的url重定向到外部站点,而不管指定的url是否具有匹配的控制器/操作。我不想弄乱现有的代码,而是使用路由来执行一些页面,并从其他页面重定向

例如:

将执行mysite.com/supports/signup

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

即使赞助商控制器包含注册和信息的操作,并且注册和信息都有现有视图

到目前为止,我还无法找到一种方法来做这件事

有什么想法吗

您可以使用来简化操作

您的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");
    }
}
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>
  <add key="UseAttributeRouting" value="true" />
</appSettings>
然后,您可以添加如下操作:

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");
    }
}
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>
  <add key="UseAttributeRouting" value="true" />
</appSettings>
编辑一个

如果您不想使用属性路由,您仍然需要该操作,但您的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");
    }
}
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>
  <add key="UseAttributeRouting" value="true" />
</appSettings>
在这样的路由中,如果找到匹配项,则忽略其余路由。所以,你要把最具体的路线放在上面,把最一般的路线放在下面

编辑两个(基于评论)

您可以在Web.config中放置一个简单的appsettings,如下所示:

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");
    }
}
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>
  <add key="UseAttributeRouting" value="true" />
</appSettings>
浏览器有时会缓存这些重定向,因此如果更改这些设置,您可能希望建议清除浏览器缓存。检查此项以清除Chrome的缓存

您有两个选择 i) 这是编写由自定义IRoutehandler安装的自定义mvchandler的完美用例。你可以遵循这个例子


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

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

routes.IgnoreRoute(“yourwebpage.aspx”)

步骤2:在名为“yourwebpage.aspx”的网站根目录下创建新文件

步骤3:在你的网页内。aspx放入:

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

这很简单

创建一个动作

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

非常感谢你!属性路由可能是我的最佳选择。我在帖子中没有提到的一点是,我希望能够通过web.config应用程序设置关闭重定向功能。如果我使用属性路由,我可以通过有条件地包括routes.mapmvcattributteroutes()来关闭If;在我的代码路径中?这个网站经营着一个孤儿院赞助项目,我所有的时间都是捐赠的。感谢您抽出时间和专业知识@TracyFletcher不客气!:)你能抽出时间真是太棒了,我很乐意帮忙。请检查我的编辑。谢谢,我感谢你的好话,也感谢你的帮助。是的,你的编辑正是我所想的。最后一个问题是,我可以在一个控制器操作上使用多个属性吗?例如[Route(“赞助商/信息”)][Route(“AboutUs/ContactUs”)]等。是的,您可以有多个这样的路由属性。非常感谢您的帮助!我用现有的控制器和动作代码捕获了一个完全有效的mvc路径。如果我的自定义mvc处理程序出现在global.asax中的注册路由列表中,在常规处理程序之前,并重定向到外部url,我应该可以走了,对吗?谢谢!重定向工作正常。不过,出现了一个奇怪的副作用。我的ActionLink解析不正确。My Global.asax.cs有许多条目,如:routes.Add(“ContactUs”,new hardrirect(“ContactUs”,strRedirectURL,new hardrirecthandler());现在,我的ActionLinks,比如:正在生成这样的链接,而不是