ASP.Net MVC重定向具体路线为外部网站 [英] ASP.Net MVC Redirect Specific Routes to External Site

查看:981
本文介绍了ASP.Net MVC重定向具体路线为外部网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用简单的标准路由方案的功能很好的MVC ASP.Net网站:

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.

我需要做的是建立路线我的看法功能/控制器,动作和无论指定的URL是否有匹配的控制器/动作重定向的其他网址到外部网站是什么。我不想与现有的code一塌糊涂,但使用路由来执行一些页面和别人重定向。

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.

例如:

mysite.com/sponsors/signup将被执行

mysite.com/sponsors/signup would be executed

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

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.

任何想法?

推荐答案

您可以使用的属性路由,使其更容易。

You can use attribute routing to make it easier.

您RouteConfig看起来象下面这样:

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

编辑一个

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

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

编辑两个(基于评论)

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

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

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

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

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

浏览器缓存,有时这些重定向,所以你可能想推荐清除浏览器缓存,如果你更改这些设置。勾选此超级用户帖子清除缓存为Chrome。

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.

这篇关于ASP.Net MVC重定向具体路线为外部网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆