如何在.Net Core 2.2中解析Microsoft.AspNetCore.Mvc.Internal.ActionContext.GetNormalizedRouteValue

时间:2019-06-04 13:33:48

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

Microsoft.AspNetCore.Mvc.Internal.ActionContext.GetNormalizedRouteValue()不存在

我正在.net Core 2.2中实现自定义Razor ViewEngine。

MSDN指出{。{1}}方法存在于.Net Core 2.2的Microsoft.AspNetCore.Mvc.Internal中,但是当我编译或检查程序集时,它似乎不存在。

我的代码

我正在使用的代码上下文为GetNormalizedRouteValue()

context.GetNormalizedRouteValue(AREA_KEY)

Microsoft的API

在检查Microsoft API时,似乎仅存在以下方法签名。

public class CustomViewEngine : IViewEngine
{
 public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage)
        {
            var controllerName = context.GetNormalizedRouteValue(CONTROLLER_KEY);
            var areaName = context.GetNormalizedRouteValue(AREA_KEY);

            var checkedLocations = new List<string>();
            foreach (var location in _options.ViewLocationFormats)
            {
                var view = string.Format(location, viewName, controllerName);
                if (File.Exists(view))
                {
                    return ViewEngineResult.Found("Default", new CustomView(view, _customViewRendering));
                }
                checkedLocations.Add(view);
            }

            return ViewEngineResult.NotFound(viewName, checkedLocations);
        }

我已经检查过MSDN's documentation,它表明它应该存在。

错误

  

'ActionContext'不包含'GetNormalizedRouteValue'的定义,找不到可以接受的扩展方法'GetNormalizedRouteValue'接受类型为'ActionContext'的第一个参数(您是否缺少using指令或程序集引用?)

2 个答案:

答案 0 :(得分:1)

GetNormalizedRouteValue()方法是static,并且是根据Microsoft.AspNetCore.Mvc.Internal.NormalizedRouteValue类型定义的。

尝试以下方法:

var controllerName = NormalizedRouteValue.GetNormalizedRouteValue(context, CONTROLLER_KEY);

请参见MSDN

答案 1 :(得分:1)

您需要对代码进行一些更改 确保您正在使用

Microsoft.AspNetCore.Mvc.Razor

GetNormalizedRouteValue方法现已公开。因此,您可以调用RazorViewEngine.GetNormalizedRouteValue方法

替换下一行

 var controllerName = context.GetNormalizedRouteValue(CONTROLLER_KEY);

您的方法将是这样

 public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage)
  {
   var controllerName = RazorViewEngine.GetNormalizedRouteValue(context, CONTROLLER_KEY);
   var areaName = RazorViewEngine.GetNormalizedRouteValue(context, AREA_KEY)             
                //your code
    }

Mircosoft API

namespace Microsoft.AspNetCore.Mvc.Razor
{

    public class RazorViewEngine : IRazorViewEngine, IViewEngine
    {
        public static readonly string ViewExtension;
        protected IMemoryCache ViewLookupCache { get; }
        public static string GetNormalizedRouteValue(ActionContext context, string key);
        public RazorPageResult FindPage(ActionContext context, string pageName);
        public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage);
        public string GetAbsolutePath(string executingFilePath, string pagePath);
        public RazorPageResult GetPage(string executingFilePath, string pagePath);
        public ViewEngineResult GetView(string executingFilePath, string viewPath, bool isMainPage);
    }
}