ASP.NET Core MVC 中的属性路由
ASP.NET Core MVC 中的属性路由
属性路由使用时,需要在Configure里面注入app.UseMvc();
,并且取消默认路由设置
- 使用属性路由,我们使用Route()属性来定义路由。
- 路由属性可应用于控制器或控制器中的操作方法上。
- 使用属性路由时,路由属性需要设置在实际使用它们的操作方法上方。
- 属性路由和传统路由相比提供了更多的灵活性。
属性路由支持层次目录结构
请注意,如果操作方法上的路由模版以/或~/开头,控制器的路由模版不会于操作方法的路由模版组合在一起。
[Routes("Home")]
public class HomeController : Controller
{
private redonly IStudentRepository _studentRepository;
//使用构造函数注入的方式注入IStudentRepository
public HomeController(IStudentRepository studentRepository)
_studentRepository = studentRepository;
}
[Route("/")]
[Route("")]
[Route("Index")]
public IActionResult Index()
IEnumerable<Student> students = _studentRepository.GetAllStudents();
return View(students);
}
//?使路由模版中的id参数可选,如果要使它为必选,删除?即可
[Route("Details/{id?}")]
// ?使id方法参数可以为空
public IActionResult Index()
{
//实例化HomeDetailViewModel并存储Student详细信息和PageTitle
HomeDetailsViewModel homeDetailsViewModel = new HomeDetailsViewModel()
{//如果“id”为null,则使用1,否则使用路由中传递过来的值
Student = _studentRepository.GetSudent(id?1),
PageTitle = "学生详细信息"
};
return View(homeDetailsViewModel);
}
}
在属性中自定义路由
[Route("[controller]")]
public class DepartmentsController : Controller
{
[Route("[action]")]
public string List()
{
return "DepartmensController控制器中的List()方法";
}
[Route("[action]")]
public string Details()
{
return "DepartmentsController控制器中的Details()方法";
}
}
重命名控制器或操作方法的名称的时候不需要去修改路由模版规则。
属性路由的几种添加方式
基本用法:
public class HomeController : Controller
{
[Route("")]
[Route("Home")]
[Route("Home/Index")]
public ActionResult Index()
{
IEnumerable<Student> students = _studentRepository.GetAllStudents();
return View(students);
}
[Route("Home/Details/{id?}")]
public IActionResult Details(int? id)
{
// 实例化HomeDetailsViewModel并存储Student详细信息和PageTitle
HomeDetailsViewModel homeDetailsViewModel = new HomeDetailsViewModel()
{
Student = _studentRepository.GetStudent(id??1),
PageTitle = "Student Details"
};
// 讲ViewModel对象传递给View()方法
return View(homeDetailsViewModel);
}
}
省略Home:
[Route("Home")]
public class HomeController : Controller
{
private readonly IStudentRepoository _studentRepository;
public HomeController(IStudentRepoository studentRepository)
{
_studentRepository = studentRepository;
}
[Route("")]
[Route("Index")]
[Route("~/")]
public ActionResult Index()
{
IEnumerable<Student> students = _studentRepository.GetAllStudents();
return View(students);
}
[Route("Details/{id?}")]
public IActionResult Details(int? id)
{
// 实例化HomeDetailsViewModel并存储Student详细信息和PageTitle
HomeDetailsViewModel homeDetailsViewModel = new HomeDetailsViewModel()
{
Student = _studentRepository.GetStudent(id??1),
PageTitle = "Student Details"
};
// 讲ViewModel对象传递给View()方法
return View(homeDetailsViewModel);
}
}
进一步用法:
[Route("[controller]")]
public class HomeController : Controller
{
private readonly IStudentRepoository _studentRepository;
public HomeController(IStudentRepoository studentRepository)
{
_studentRepository = studentRepository;
}
[Route("")]
[Route("[action]")]
[Route("~/")]
public ActionResult Index()
{
IEnumerable<Student> students = _studentRepository.GetAllStudents();
return View(students);
}
[Route("[action]/{id?}")]
public IActionResult Details(int? id)
{
// 实例化HomeDetailsViewModel并存储Student详细信息和PageTitle
HomeDetailsViewModel homeDetailsViewModel = new HomeDetailsViewModel()
{
Student = _studentRepository.GetStudent(id??1),
PageTitle = "Student Details"
};
// 讲ViewModel对象传递给View()方法
return View(homeDetailsViewModel);
}
}
省略action
[Route("[controller]/[action]")]
public class HomeController : Controller
{
private readonly IStudentRepoository _studentRepository;
public HomeController(IStudentRepoository studentRepository)
{
_studentRepository = studentRepository;
}
[Route("")]
[Route("~/")]
public ActionResult Index()
{
IEnumerable<Student> students = _studentRepository.GetAllStudents();
return View(students);
}
[Route("{id?}")]
public IActionResult Details(int? id)
{
// 实例化HomeDetailsViewModel并存储Student详细信息和PageTitle
HomeDetailsViewModel homeDetailsViewModel = new HomeDetailsViewModel()
{
Student = _studentRepository.GetStudent(id??1),
PageTitle = "Student Details"
};
// 讲ViewModel对象传递给View()方法
return View(homeDetailsViewModel);
}
}
将传统路由和属性路由结合使用
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILogger<Startup> logger)
{
app.UseStaticFiles();
//启动MVC默认路由
//app.UseMvcWithDefaultRoute();
// 启动自定义路由
app.UseMvc(routes =>
{
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
}