【问题标题】:MVC route for child parameters子参数的 MVC 路由
【发布时间】:2019-01-28 13:10:22
【问题描述】:

我写了一条路线:

        routes.MapRoute(
            name: "LoadDefaultPage",
            url: "Load",
            defaults: new { controller = "Load", action = "Index" }
        );

它适用于http://localhost/Load。但是我需要对http://localhost/Load/blahttp://localhost/Load/bla/bla/http://localhost/Load/bla/bla/bla等做同样的操作。如何描述呢?

【问题讨论】:

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


【解决方案1】:

使用此代码解决了它(感谢 Stephen Muecke):

        routes.MapRoute(
            name: "LoadDefaultPage",
            url: "Load/{*tmp}",
            defaults: new { controller = "Load", action = "Index" }
        );

【讨论】:

    【解决方案2】:

    您可以创建具有不同路由前缀的控制器。例如

    [RoutePrefix("Load")]
    public class LoadController : ApiController
    {
    
    }
    
    [RoutePrefix("Load/bla")]
    public class LoadBlaController : ApiController
    {
    
    }
    

    或者,如果您希望 http://localhost/Load/bla 成为方法,请使用:

    [RoutePrefix("Load")]
    public class LoadController : ApiController
    {
        [HttpGET, Route("bla")]
        public string Bla()
        {
            return "bla";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-21
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多