0

我在我的MVC站點中設置了自定義路由。我無法弄清楚如何重定向到它。這裏是我的自定義路由代碼:重定向到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 } 
     ); 
    } 
} 

這是工作的罰款。如果我在瀏覽器網址中放置了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

回答

1

嘗試

RedirectToAction("Index", "Dashboard", new { permalink = "Joes-Page"}); 
+0

這花了我在這裏:'http://www.myexample.com/Dashboard ' – Icemanind

+0

這個問題其實是另一回事。這工作。 – Icemanind