简体   繁体   English

MVC自定义路由问题

[英]MVC custom routing issue

i am using MVC 5, and trying to create custom routing in Route.Config ie Home but when i run application, it takes me straight to my custom routing which i don't want. 我正在使用MVC 5,并尝试在Route.Config(即Home)中创建自定义路由,但是当我运行应用程序时,它将直接转到我不想要的自定义路由。 the default is login, success of which should lead to Dashboard/Home (controller/Action) but I want to use only Home (Action Name only). 默认值为登录名,成功登录将导致显示板/主页(控制器/操作),但我只想使用主页(仅操作名)。 Many Thanks 非常感谢

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //---In order to use only Action Title in URL--//
        routes.MapRoute(
            "Home",
            "{action}/{id}",
             new { controller = "Dashboard", action = "Home", id = UrlParameter.Optional }
            );

        //-----default routing---//
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Admin", action = "Login", id = UrlParameter.Optional }
        );
    }
}

Try this: 尝试这个:

   routes.MapRoute(
        "Home",
        "Home/{id}",
         new { controller = "Dashboard", action = "Home", id = UrlParameter.Optional }
        );

If you want to navigate within action to a new action of a different controller then you have to mention like below. 如果要在动作内导航到其他控制器的新动作,则必须像下面提到的那样。

If you want to navigate to Home from your login then you really dont have to change the route. 如果您想从登录名导航到首页,那么您实际上不必更改路线。 In your login action on success call 在成功通话的登录操作中

RedirectToAction(String, String)    Redirects to the specified action using the action name and controller name.

Provide 提供

RedirectToAction("Home", "Dashboard");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM