简体   繁体   English

子参数的MVC路由

[英]MVC route for child parameters

I wrote a route: 我写了一条路线:

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

and it works fine for http://localhost/Load . 它适用于http:// localhost / Load But I need to do the same action for http://localhost/Load/bla , http://localhost/Load/bla/bla/ , http://localhost/Load/bla/bla/bla etc. How to describe it? 但是我需要对http:// localhost / Load / blahttp:// localhost / Load / bla / bla /http:// localhost / Load / bla / bla / bla等执行相同的操作。如何描述它?

Solved it using this code (thanks for Stephen Muecke): 使用以下代码解决了该问题(感谢Stephen Muecke):

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

You can create Controllers with different Routprefixes. 您可以使用不同的Routprefix创建控制器。 eg 例如

[RoutePrefix("Load")]
public class LoadController : ApiController
{

}

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

}

Or if you want http://localhost/Load/bla to be a Method use: 或者,如果您希望将http:// localhost / Load / bla用作方法,请使用:

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

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

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