【问题标题】:Defining static routes other than default to similar route in home [duplicate]将默认路由以外的静态路由定义为家庭中的类似路由[重复]
【发布时间】:2017-06-14 04:09:09
【问题描述】:

我在我的 .NET MVC 5 应用程序中设置了一个默认路由,如下所示:

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

所以现在每当我访问我的网站时:

localhost:59920 

它从家里打开了索引控制器。

现在我在同一个控制器中有更多方法,例如:

Register, Login and such 

如何设置路线,使其看起来像这样:

localhost:59920/Login

localhost:59920/Register

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-5 routing asp.net-mvc-routing


    【解决方案1】:

    使用属性路由

    public class AccountController : Controller
    {
      [Route("Login")]
      public ActionResult Login()
      {
         return View();
      }
    }
    

    使用传统路由

    routes.MapRoute(
        name: "Login",
        url: "Login",
        defaults: new { controller = "Account", action = "Login" }
    );
    
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    

    现在当您请求yourSite/Login 时,AccountController 中的 Login 操作方法将处理该问题。

    【讨论】:

      猜你喜欢
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      相关资源
      最近更新 更多