asp.net-mvc 使用 ASP.NET MVC MapRoute 重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21200545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:27:34  来源:igfitidea点击:

Redirect with ASP.NET MVC MapRoute

asp.net-mvcasp.net-mvc-routing

提问by Khachatur

On my site, I have moved some images from one folder to another.

在我的网站上,我已将一些图像从一个文件夹移动到另一个文件夹。

Now, when I receive a request for old images '/old_folder/images/*' I want to make a permanent redirect to new folder with these images '/new_folder/images/*'

现在,当我收到对旧图像 '/old_folder/images/*' 的请求时,我想使用这些图像 '/new_folder/images/*' 永久重定向到新文件夹

For example:

例如:

/old_folder/images/image1.png => /new_folder/images/image1.png

/old_folder/images/image2.jpg => /new_folder/images/image2.jpg

I have added a simple redirect controller

我添加了一个简单的重定向控制器

public class RedirectController : Controller
{
    public ActionResult Index(string path)
    {
        return RedirectPermanent(path);
    }
}

Now I need to setup proper routing, but I don't know how to pass the path part to the path parameter.

现在我需要设置正确的路由,但我不知道如何将路径部分传递给路径参数。

routes.MapRoute("ImagesFix", "/old_folder/images/{*pathInfo}", new { controller = "Redirect", action = "Index", path="/upload/images/????" }); 

Thanks

谢谢

回答by Volodymyr Bilyachat

I would do in next way

我会用下一种方式

routes.MapRoute("ImagesFix", "/old_folder/images/{path}", new { controller = "Redirect", action = "Index" }); 

and in controller like that

在这样的控制器中

public class RedirectController : Controller
{
    public ActionResult Index(string path)
    {
        return RedirectPermanent("/upload/images/" + path);
    }
}

回答by J_hajian_nzd

first download and install RouteMagic package from this link, then redirect your old address to the new address Like the below code :

首先从此链接下载并安装RouteMagic包,然后将您的旧地址重定向到新地址,如下代码所示:

var NewPath = routes.MapRoute("new", "new_folder/images/{controller}/{action}");
var OldPath = routes.MapRoute("new", "old_folder/images/{controller}/{action}");
routes.Redirect(OldPath ).To(NewPath );

for more information please check out the following link Redirecting Routes To Maintain Persistent URLs

有关更多信息,请查看以下链接 重定向路由以维护持久 URL

回答by Matt Kemp

Answer above using RouteMagic is a good idea, but the example code is wrong (it's included in Phil's post as a badexample).

上面使用 RouteMagic 的答案是一个好主意,但示例代码是错误的(它作为一个示例包含在 Phil 的帖子中)。

From the RouteMagic Github demo site global.asax.cs:

来自 RouteMagic Github 演示站点global.asax.cs

// Redirect From Old Route to New route
var targetRoute = routes.Map("target", "yo/{id}/{action}", new { controller = "Home" });
routes.Redirect(r => r.MapRoute("legacy", "foo/{id}/baz/{action}")).To(targetRoute, new { id = "123", action = "index" });

If you specify two routes, you will be setting up an extra mapping that will catch URLs which you don't want.

如果您指定两个路由,您将设置一个额外的映射来捕获您不想要的 URL。