将自定义命名参数从URL映射到NET Core MVC中的变量

时间:2018-01-12 16:43:36

标签: c# asp.net-core-mvc

如果我有像这样的路由器:

app.UseMvc(config => {
    config.MapRoute(
        name: "route",
        template: "{controller}/{action}/{id?}"
    );
});

是否可以调用参数名为id以外的方法,并且仍然可以绑定URL中的值。

public void SetHeight(int height){
    //height do not binds value from URL to height
}

public void SetWidth(int width){
    //height do not binds value from URL to width
}

public void SetHeight(int id){
    //works as expected however I would like to have different 
    //parameters name for different action on controller
}

public void SetWidth(int id){
    //works as expected however I would like to have different 
    //parameters name for different action on controller
}

是否有一些注释或类似的东西可以启用不同的参数名称? 类似的东西:

public void SetHeight([FromUrl(name = "id")]int height){
     ...
}

1 个答案:

答案 0 :(得分:2)

你可以试试这个:

    [Route("[controller]/[action]/{height}")]
    public void SetHeight(int height)
    {
        //height do not binds value from URL to height
    }


    [Route("[controller]/[action]/{width}")]
    public void SetWidth(int width)
    {
        //height do not binds value from URL to width
    }