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

标签 asp.net-mvc redirect controller routing

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

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

我的客户想将静态页面重定向到辅助站点,以便他们可以随意编辑它们(模板样式)。实际执行某些操作的页面将保留在原始站点上。

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

例如:

mysite.com/sponsors/signup 将被执行

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

即使赞助商 Controller 包含注册和信息的操作,并且注册和信息都有现有的 View 。

到目前为止,我一直无法想出一个办法来做到这一点。

有什么想法吗?

最佳答案

您可以使用 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 }
        );


    }
}

在这样的路由中,如果找到匹配项,则忽略其余路由。因此,您希望将最具体的路线放在顶部,将最一般的路线放在底部

编辑二(根据评论)

您可以像下面这样在 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 的缓存。

关于asp.net-mvc - ASP.Net MVC 将特定路由重定向到外部站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29690317/

相关文章:

asp.net-mvc - 我的模型应该住在哪里? Web 层还是数据层? (MVC + NHibernate)

asp.net-mvc - 使用 NHibernate 和 Autofac 管理多个数据库

shell - 重定向命令输出

php - 方法 Illuminate\Routing\Route::resource 不存在。在 Macroable.php 第 78 行

c# - 使用 Xbox Controller 播放动画

java - 具有 Spring 形式的 JSP 不调用 Spring 4.x Controller

asp.net-mvc - 如果选择了 DropDownList 的定义选项,则禁用 CheckBox

asp.net-mvc - 在客户端清除outputcache

javascript - 使用 JavaScript 书签打开当前打开站点的新网页

php - 如何从 slim Rest API 函数重定向到 codeigniter Controller 函数?