Kei*_*ill 2 asp.net asp.net-mvc-routing asp.net-core
我有几个用于创建导航栏按钮的HtmlHelper扩展方法 - 一个用于上下文相关帮助链接.在我的扩展方法中,我需要知道当前控制器和动作的名称,例如:
var currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
var currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
Run Code Online (Sandbox Code Playgroud)
我可以在ASP.NET 5中使用什么来获取此信息,因为RouteData上没有GetRequiredString()方法了?
你可以自己创建一个扩展.
namespace Microsoft.AspNet.Mvc
{
public static class HelperExtensions
{
public static string GetRequiredString(this RouteData routeData, string keyName)
{
object value;
if(!routeData.Values.TryGetValue(keyName, out value))
{
throw new InvalidOperationException($"Could not find key with name '{keyName}'");
}
return value?.ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1328 次 |
| 最近记录: |