I'm trying to include .css files based on the controller and action like this in my _Layout.cshtml
@{
var actionCssName =
$"{ViewContext.RouteData.Values["Controller"]}." +
$"{ViewContext.RouteData.Values["Action"]}.css";
}
<link href="/css/@actionCssName" rel="Stylesheet" type="text/css" />
to reference a .css file with such names as: css/Controller.Action.css
At the same time, the action in my controller does not use the default name but is decorated with HttpGet:
[HttpGet("report")]
public IActionResult GetReport()
{
return View("Report", new ReportBody { ... });
}
Unfortunatelly ViewContext.RouteData.Values["Action"] returns the name GetReport instead of report which does not match the .css file because the final name is css/TestController.GetReport.css. I'd rather not rename it if it is possible to get the alias for the action.
Or am I doing this completely wrong and there is another way that actually works?
You either have to change your method name from GetReport to Report
[HttpGet("report")]
public IActionResult Report()
{
return View("Report", new ReportBody { ... });
}
Or you can use ActionName Attribute
[HttpGet("report")]
[ActionName("Report")]
public IActionResult GetReport()
{
return View("Report", new ReportBody { ... });
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With