0

If I have a router like:

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

Is it possible to call methods that have parameter named other than id and still bind value from 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
}

Is there some annotation or something similar to enable different parameter names? Something like:

public void SetHeight([FromUrl(name = "id")]int height){
     ...
}
2
  • Read up on attribute routing Commented Jan 12, 2018 at 16:48
  • The normal is to bind them as url parameters (e.g. mysite.com/Controller/SetHeight?height=xxx) Commented Jan 12, 2018 at 16:57

1 Answer 1

4

You could try this:

    [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
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.