Skip to content

Instantly share code, notes, and snippets.

@kneerunjun
Last active January 3, 2016 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kneerunjun/5114d17d0291912160e9 to your computer and use it in GitHub Desktop.
Save kneerunjun/5114d17d0291912160e9 to your computer and use it in GitHub Desktop.
understanding attribute routing in MVC4 ASP
[RoutePrefix("home")]
[Route("{action=index}")] //this would assign the default action from the controller
public class HomeController:Controller
{
//here despite not giving route attribute the controller woudl still pick up this action as the default
public ActionResult Default(){
}
[Route("users")]
//route /home/users
public ActionResult GetUsers(){
//you can very well do what you want from here, but see the route mapping on the top
}
[Route("users/{id:int}/skills")]
//route : /home/users/1/skills
//..see how the user can be singled out and then skills are obtained.
//this becomes a bit of trouble in conventional routing
public ActionResult GetSkillsOfUser(id){
/*this woudl find the user first with the id given , observe that the id in the url template and the id in the parameter is the same*/
//once the user is furbuished we can then proceed to the skills of the user
}
[Route("users/{id:int}/skills/{like:alpha}")]
//route : /home/users/1/skills/Net
public ActionResult GetLikelySkillsOfUser(id, like){
//this is capable of getting not only the user by the id but also the likely skills of the user from the phrase to be searched
}
//and this is how you get to have optional params in the url
[Route("skills/{top:int?}")]
public ActionResult GetTopSkills(top=10){
//you can get all those skills from here , if the value for the top is not specified then 10 skills would be returned
}
[Route("~/categories")]
//this is the escaping route which does not want to be under /home location
//route : /categories
public ActionResult GetCategories(){
//here we can get the categories from the data base but one needs to see the route under which it is mapped
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment