我正在使用RedirectToAction
重定向到不同的视图,但是我在IIS中的某个站点中使用了一个应用程序,所以有一个前缀必须转到该路径。
当我做RedirectToAction
它只是重定向根。
我需要它来尊重当前路径并沿着这些路线重定向。
例如:
我在https://localhost:8085/app/custompath/controller1/action
我打电话给return RedirectToAction("controller2", "action");
我被重定向到
https://localhost:8085/controller2/action
代替
https://localhost:8085/app/custompath/controller2/action
如果你在Starup.cs
中有这样的东西:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "custom",
template: "app/custompath/{controller=Home}/{action=Index}");
});
然后你可以这样做重定向:
var url = Url.RouteUrl("custom", new { controller = "controller2", action = "action" });
return Redirect(url);
顺便说一句,如果你需要,.NET Core内置了URL Rewrite。
RedirectToAction
,或任何类似的方法,允许您根据路径信息(如控制器,操作,名称等)生成URL,仅适用于上下文中当前运行的应用程序。您不能使用它们为其他应用程序中的路径生成URL。
您可以使用简单的Redirect
和字符串URL,但是您可以生成所需的URL。典型方法是在应用程序设置中设置外部应用程序的基本路径。