asp.net-mvc - 如何在 MVC 5 中将多个路由设置为同一操作方法?

标签 asp.net-mvc angular

我正在使用 MVC 5 和 Angular 2 开发单页应用程序。

我的要求是我需要为同一 Controller /操作方法设置多个路由。

如果用户输入 url http://localhost:xxxx/home/index ,它将针对“home” Controller 的“index”操作方法。

我也 希望“http://localhost:xxxx/products”和“http://localhost:xxxx/categories”指向“home” Controller 的“index”操作方法。

注意:“产品”和“类别”都不是 Controller 或操作方法。

最佳答案

添加以下 2 条路线即可完成任务。

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

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


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

关于asp.net-mvc - 如何在 MVC 5 中将多个路由设置为同一操作方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41909047/

相关文章:

asp.net-mvc - 我应该如何为需要在 ASP.NET MVC 中显示的分层数据构建我的 ViewModel?

jquery - 如何导航/聚焦到 Angular2 中的下一个项目?

asp.net-mvc - ASP.NET MVC帐户 Controller 使用指南?

使用 iPhone UIWebView 时的 Asp.Net 表单例份验证

javascript - 类型错误 : Cannot read property 'push' of undefined-IONIC2

angular - 将对象的部分属性中的值分配给该对象会导致类型错误

angular - 带有 @Host 参数的单元测试 Angular 9 组件无法解析参数

javascript - 如何在 Angular 中使用 Chrome Extension Api?

c# - 如何在asp.net mvc 3中制作不同的name属性名称?

javascript - 跨多个解决方案的通用 JavaScript 文件