【问题标题】:shorten URL with routing? [closed]用路由缩短 URL? [关闭]
【发布时间】:2015-12-28 12:14:10
【问题描述】:

mvc路由中的url可以缩短吗?

//FieldController
public ActionResult Field()

//Views
Field

所以 URL 将是 ...Field/Field

是否可以缩短/删除结束字段。所以只有...字段

是不是和 Global 中的 RegisterRoutes 有关系

eg:
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Public", action = "Index", id = UrlParameter.Optional } // Parameter defaults
       );

【问题讨论】:

标签: c# html asp.net-mvc razor model-view-controller


【解决方案1】:

是的。在控制器路由上方添加一个新的显式路由。

routes.MapRoute("FieldRoute", "field", new { controller = "FieldController", action = "Field" });

【讨论】:

    【解决方案2】:

    似乎与以下问题重复:Set default action (instead of index) for controller in ASP.NET MVC 3

    基于以下代码应该适合您:

    routes.MapRoute(
        "Field",
        "Field/{action}",
        new { controller = "Field", action = "Field" }
    );
    
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
    );
    

    【讨论】:

      猜你喜欢
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 2014-04-23
      相关资源
      最近更新 更多