【问题标题】:How to resolve Microsoft.AspNetCore.Mvc.Internal.ActionContext.GetNormalizedRouteValue in .Net Core 2.2如何在 .Net Core 2.2 中解决 Microsoft.AspNetCore.Mvc.Internal.ActionContext.GetNormalizedRouteValue
【发布时间】:2019-06-04 13:33:48
【问题描述】:

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

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

MSDN 声明 GetNormalizedRouteValue() 方法存在于 .Net Core 2.2 的 Microsoft.AspNetCore.Mvc.Internal 中,但在我编译或检查程序集时它似乎不存在。

我的代码

我使用它的代码上下文是context.GetNormalizedRouteValue(AREA_KEY)

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);
        }

微软的 API

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

namespace Microsoft.AspNetCore.Mvc
{

    public class ActionContext
    {

        public ActionContext();

        public ActionContext(ActionContext actionContext);

        public ActionContext(HttpContext httpContext, RouteData routeData, ActionDescriptor actionDescriptor);

        public ActionContext(HttpContext httpContext, RouteData routeData, ActionDescriptor actionDescriptor, ModelStateDictionary modelState);

        public ActionDescriptor ActionDescriptor { get; set; }

        public HttpContext HttpContext { get; set; }

        public ModelStateDictionary ModelState { get; }

        public RouteData RouteData { get; set; }
    }
}

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

错误

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

【问题讨论】:

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


    【解决方案1】:

    GetNormalizedRouteValue() 方法是 static,并在 Microsoft.AspNetCore.Mvc.Internal.NormalizedRouteValue 类型上定义。

    试试这个:

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

    MSDN

    【讨论】:

    • 这不是internal偶然吗?
    • @WiktorZychla,我实际上指的是在您指向的不同名称空间上定义的名称。请参阅 github.com/aspnet/AspNetCore/blob/v2.2.1/src/Mvc/src/… - Microsoft.AspNetCore.Mvc.Internal.NormalizedRouteValue 类型及其 GetNormalizedRouteValue 方法都是 public(对于 v2.2,由 OP 使用)
    【解决方案2】:

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

    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
        }
    

    微软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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-25
      • 2019-05-09
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多