Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get action alias set by HttpGet to include a css file with the same name?

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?

like image 517
t3chb0t Avatar asked Jan 29 '26 21:01

t3chb0t


1 Answers

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 { ... });
}
like image 138
Kahbazi Avatar answered Feb 01 '26 12:02

Kahbazi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!