Routing mvc dotnet core
In startup.cs
app.UseMvc(routes =>
{
//New Route
routes.MapRoute(
name: "about-route",
template: "about",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
with attributes
[Route("[controller]")]
public class AnalyticsController : Controller
{
[Route("Dashboard")]
public IActionResult Index()
{
return View();
}
[Route("[action]")]
public IActionResult Charts()
{
return View();
}
}
rest
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] {"hello", "world!"};
}
// POST api/values
[HttpPost]
public void PostCreate([FromBody] string value)
{
}
}
constraints
[HttpGet("{id:int}")]
public string GetById(int id)
{
return "item " + id;
}