c# - 我可以获得 HttpGet 设置的操作别名以包含同名的 css 文件吗?

标签 c# asp.net asp.net-mvc asp.net-core-mvc asp.net-core-2.0

我正在尝试包括 .css在我的 _Layout.cshtml 中基于 Controller 和操作的文件

@{
    var actionCssName = 
        $"{ViewContext.RouteData.Values["Controller"]}." +
        $"{ViewContext.RouteData.Values["Action"]}.css";
}
<link href="/css/@actionCssName" rel="Stylesheet" type="text/css" />

引用.css文件名称如下:css/Controller.Action.css

与此同时,我的 Controller 中的操作不使用默认 名称,而是用HttpGet 修饰。 :

[HttpGet("report")]
public IActionResult GetReport()
{
    return View("Report", new ReportBody { ... });
}

不幸的是ViewContext.RouteData.Values["Action"]返回名称 GetReport而不是 report这与 .css 不匹配文件,因为最终名称是 css/TestController.GetReport.css .如果可以获得操作的别名,我宁愿不重命名它。

还是我的做法完全错误,还有另一种实际可行的方法?

最佳答案

您要么必须将方法名称从 GetReport 更改为 Report

[HttpGet("report")]
public IActionResult Report()
{
    return View("Report", new ReportBody { ... });
}

或者你可以使用ActionName属性

[HttpGet("report")]
[ActionName("Report")]
public IActionResult GetReport()
{
    return View("Report", new ReportBody { ... });
}

关于c# - 我可以获得 HttpGet 设置的操作别名以包含同名的 css 文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48961502/

相关文章:

c# - Visual Studio 2010 Express多语言解决方案

c# - 如果捕获所有异常,是否需要在 try catch block 中使用 finally?

c# - 如何让浏览器后退按钮通过 AJAX 调用带你返回?

c# - asp :panel in asp.net mvc 的替代方案

asp.net-mvc - 值不能为 null 或为空。参数名称: contentPath

c# - Html.TextboxFor 整数/小数的默认值为空而不是 0

c# - BsonDocument基础数学(求和)MongoDB.Driver C#

c# - 无法从字符转换为字符串

c# - 使用 javascript 填充标签值。回发后代码后面的标签为空?

c# - 在 Code First Entity Framework (C# ASP.NET MVC 3) 中指定除 dbo 之外的 SQL 用户名