在MVC中重定向到自定义路由
本文关键字:自定义 路由 重定向 MVC | 更新日期: 2023-09-27 18:16:19
我已经在我的MVC站点设置了一个自定义路由。我有麻烦弄清楚如何重定向到它虽然。以下是我的自定义路由代码:
public class MemberUrlConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values,
RouteDirection routeDirection)
{
if (values[parameterName] != null)
{
var permalink = values[parameterName].ToString();
return string.Equals(permalink, Member.GetAuthenticatedMembersUrl(),
StringComparison.OrdinalIgnoreCase);
}
return false;
}
}
这是我的RouteConfig.cs
文件:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "MembersRoute",
url: "{*permalink}",
defaults: new { controller = "Dashboard", action = "Index" },
constraints: new { permalink = new MemberUrlConstraint() });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
工作正常。如果我在浏览器URL中输入http://www.example.com/Joes-Page
,它会正常工作。
我的问题是,我如何编程重定向到这个页面?我试过了:
RedirectToAction("Index", "Dashboard"); // Goes to http://www.myexample.com/Dashboard
RedirectToRoute("MembersRoute", new {controller = Infrastructure.Member.GetAuthenticatedMembersUrl(), action = "Index"}); // Goes to http://www.myexample.com/Home/Index
我希望能够重定向到http://www.example.com/Joes-Page
Try
RedirectToAction("Index", "Dashboard", new { permalink = "Joes-Page"});