栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Asp.Net Core中的@ Html.Action

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Asp.Net Core中的@ Html.Action

更新:从2.2.2版本开始,HttpContextAccessor将上下文保留在一个对象中(据说是为了防止请求之间的混淆),这会影响当前解决方案…因此,您需要为IHttpContextAccessor(旧版本)提供以下实现并进行注册作为一个单例:

public class HttpContextAccessor : IHttpContextAccessor{    private static AsyncLocal<HttpContext> _httpContextCurrent = new AsyncLocal<HttpContext>();    HttpContext IHttpContextAccessor.HttpContext { get => _httpContextCurrent.Value; set => _httpContextCurrent.Value = value; }}

对于asp.net core 2

using Microsoft.AspNetCore.Html;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc.Infrastructure;using Microsoft.AspNetCore.Routing;using Microsoft.Extensions.DependencyInjection;using System;using System.IO;using System.Threading.Tasks;namespace Microsoft.AspNetCore.Mvc.Rendering{    public static class HtmlHelperViewExtensions    {        public static IHtmlContent Action(this IHtmlHelper helper, string action, object parameters = null)        { var controller = (string)helper.ViewContext.RouteData.Values["controller"]; return Action(helper, action, controller, parameters);        }        public static IHtmlContent Action(this IHtmlHelper helper, string action, string controller, object parameters = null)        { var area = (string)helper.ViewContext.RouteData.Values["area"]; return Action(helper, action, controller, area, parameters);        }        public static IHtmlContent Action(this IHtmlHelper helper, string action, string controller, string area, object parameters = null)        { if (action == null)     throw new ArgumentNullException("action"); if (controller == null)     throw new ArgumentNullException("controller"); var task = RenderActionAsync(helper, action, controller, area, parameters); return task.Result;        }        private static async Task<IHtmlContent> RenderActionAsync(this IHtmlHelper helper, string action, string controller, string area, object parameters = null)        { // fetching required services for invocation var serviceProvider = helper.ViewContext.HttpContext.RequestServices; var actionContextAccessor = helper.ViewContext.HttpContext.RequestServices.GetRequiredService<IActionContextAccessor>(); var httpContextAccessor = helper.ViewContext.HttpContext.RequestServices.GetRequiredService<IHttpContextAccessor>(); var actionSelector = serviceProvider.GetRequiredService<IActionSelector>(); // creating new action invocation context var routeData = new RouteData(); foreach (var router in helper.ViewContext.RouteData.Routers) {     routeData.PushState(router, null, null); } routeData.PushState(null, new RoutevalueDictionary(new { controller = controller, action = action, area = area }), null); routeData.PushState(null, new RoutevalueDictionary(parameters ?? new { }), null); //get the actiondescriptor RouteContext routeContext = new RouteContext(helper.ViewContext.HttpContext) { RouteData = routeData }; var candidates = actionSelector.SelectCandidates(routeContext); var actionDescriptor = actionSelector.SelectBestCandidate(routeContext, candidates); var originalActionContext = actionContextAccessor.ActionContext; var originalhttpContext = httpContextAccessor.HttpContext; try {     var newHttpContext = serviceProvider.GetRequiredService<IHttpContextFactory>().Create(helper.ViewContext.HttpContext.Features);     if (newHttpContext.Items.ContainsKey(typeof(IUrlHelper)))     {         newHttpContext.Items.Remove(typeof(IUrlHelper));     }     newHttpContext.Response.Body = new MemoryStream();     var actionContext = new ActionContext(newHttpContext, routeData, actionDescriptor);     actionContextAccessor.ActionContext = actionContext;     var invoker = serviceProvider.GetRequiredService<IActionInvokerFactory>().CreateInvoker(actionContext);     await invoker.InvokeAsync();     newHttpContext.Response.Body.Position = 0;     using (var reader = new StreamReader(newHttpContext.Response.Body))     {         return new HtmlString(reader.ReadToEnd());     } } catch (Exception ex) {     return new HtmlString(ex.Message); } finally {     actionContextAccessor.ActionContext = originalActionContext;     httpContextAccessor.HttpContext = originalhttpContext;     if (helper.ViewContext.HttpContext.Items.ContainsKey(typeof(IUrlHelper)))     {         helper.ViewContext.HttpContext.Items.Remove(typeof(IUrlHelper));     } }        }    }}

它基于白羊座的反应。我更正了2.0版未编译的内容,并添加了一些调整。当前的httpcontext和当前的actioncontext有2个美化的静态值。在httpcontext中

IHttpContextFactory.Create
设置了一个,在代码中设置了actioncontext。请注意,这取决于你使用的功能
IActionContextAccessor
,并
IHttpContextAccessor
可能不会被默认注册,所以你可能需要将其添加在启动:

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

HttpContext只是一个包装器

HttpContext.Features
,因此,如果您在其中一个进行更改,则在另一个中也进行更改…我将在try
/ catch的最后部分重设我所了解的内容。

IUrlHelper
从Items缓存中删除了,因为即使构建urlHelper的actionContext不同,该值也将被重用
IUrlHelperFactory.GetUrlHelper

Asp.net Core 2.0假设您不会这样做,那么很有可能还有其他缓存的东西,因此我建议在使用此方法时要格外小心,如果不需要,请不要这样做。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/391681.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号