how to call html action in asp net core

Solutions on MaxInterview for how to call html action in asp net core by the best coders in the world

showing results for - "how to call html action in asp net core"
Jessie
29 Jun 2019
1using System;
2using System.IO;
3using System.Linq;
4using System.Threading.Tasks;
5using Microsoft.AspNetCore.Html;
6using Microsoft.AspNetCore.Http;
7using Microsoft.AspNetCore.Http.Extensions;
8using Microsoft.AspNetCore.Mvc;
9using Microsoft.AspNetCore.Mvc.Controllers;
10using Microsoft.AspNetCore.Mvc.Internal;
11using Microsoft.AspNetCore.Mvc.Infrastructure;
12using Microsoft.AspNetCore.Mvc.Razor;
13using Microsoft.AspNetCore.Mvc.Rendering;
14using Microsoft.AspNetCore.Routing;
15
16namespace Microsoft.AspNetCore.Mvc.Rendering    {
17
18    public static class HtmlHelperViewExtensions
19    {
20
21        public static IHtmlContent RenderAction(this IHtmlHelper helper, string action, object parameters = null)
22        {
23            var controller =  (string)helper.ViewContext.RouteData.Values["controller"];
24
25            return RenderAction(helper, action, controller, parameters);
26        }
27
28        public static IHtmlContent RenderAction(this IHtmlHelper helper, string action, string controller, object parameters = null)
29        {
30            var area = (string)helper.ViewContext.RouteData.Values["area"];
31
32            return RenderAction(helper, action, controller, area, parameters);
33        }
34
35        public static IHtmlContent RenderAction(this IHtmlHelper helper, string action, string controller, string area, object parameters = null)
36        {
37            if (action == null)
38                throw new ArgumentNullException("action");
39
40            if (controller == null)
41                throw new ArgumentNullException("controller");
42
43            if (area == null)
44                throw new ArgumentNullException("area");
45
46            var task = RenderActionAsync(helper, action, controller, area, parameters);
47
48            return task.Result;
49        }
50
51        private static async Task<IHtmlContent> RenderActionAsync(this IHtmlHelper helper, string action, string controller, string area, object parameters = null)
52        {
53            // fetching required services for invocation
54            var currentHttpContext = helper.ViewContext?.HttpContext;
55            var httpContextFactory = GetServiceOrFail<IHttpContextFactory>(currentHttpContext);
56            var actionInvokerFactory = GetServiceOrFail<IActionInvokerFactory>(currentHttpContext);
57            var actionSelector = GetServiceOrFail<IActionSelectorDecisionTreeProvider>(currentHttpContext);
58
59            // creating new action invocation context
60            var routeData = new RouteData();
61            var routeParams = new RouteValueDictionary(parameters ?? new { });
62            var routeValues = new RouteValueDictionary(new { area = area, controller = controller, action = action });
63            var newHttpContext = httpContextFactory.Create(currentHttpContext.Features);
64
65            newHttpContext.Response.Body = new MemoryStream();
66
67            foreach (var router in helper.ViewContext.RouteData.Routers)
68                routeData.PushState(router, null, null);
69
70            routeData.PushState(null, routeValues, null);
71            routeData.PushState(null, routeParams, null);
72
73            var actionDescriptor = actionSelector.DecisionTree.Select(routeValues).First();
74            var actionContext = new ActionContext(newHttpContext, routeData, actionDescriptor);
75
76            // invoke action and retreive the response body
77            var invoker = actionInvokerFactory.CreateInvoker(actionContext);
78            string content = null;
79
80            await invoker.InvokeAsync().ContinueWith(task => {
81                if (task.IsFaulted)
82                {
83                    content = task.Exception.Message;
84                }
85                else if (task.IsCompleted)
86                {
87                    newHttpContext.Response.Body.Position = 0;
88                    using (var reader = new StreamReader(newHttpContext.Response.Body))
89                        content = reader.ReadToEnd();
90                }
91            });
92
93            return new HtmlString(content);
94        }
95
96        private static TService GetServiceOrFail<TService>(HttpContext httpContext)
97        {
98            if (httpContext == null)
99                throw new ArgumentNullException(nameof(httpContext));
100
101            var service = httpContext.RequestServices.GetService(typeof(TService));
102
103            if (service == null)
104                throw new InvalidOperationException($"Could not locate service: {nameof(TService)}");
105
106            return (TService)service;
107        }
108    }
109}
queries leading to this page
use plsql with dotnet core 2cnet core 3 html action encoding 2faction method in asp net core net core in actionmvc net core action result net core html actionasp net core mvc see action methodsasp net core mvc javascript get 40html actionhtml action asp net core 5 40html action in net corepage action c 23 net core 5waht is an action code in asp net core mvc net core action in action net core 40html action net core 3 1 40html actionhtml action in net corehtml action in net core mvccontainerize an asp net core 3 1 application with dockerhtml action in mvc corehtml action in asp net core net core action resultdotnet core in actionasp net mvc html action renders hole pageaction result in net core web apinet core in actionhtml action asp net core net core 3 html action encoding 2fadd action in the view in asp net core api net core 5 40html actionasp net core mvc html actionmvc core 3 1 render action to stringhtml action net coredotnet call an action htmlaction net c 23 net corehow to write api 27s in dotnet coreasp net core action method typeasp net core action on viewasp net core in actioncreated aspnet core action resultasp net core call action from javascriptaspnet code html action alternativerender actionin a view asp net corecall a action from layout net coremake a action in asp net core 40html action in net core 3 1 net core 3 1 mvc html action 40html action net core mvc net core html action viewpurpose of ascx in asp netasp net core mvc html actionasp net core 3a crud with react js and entity framework coreasp action in asp net coreaction asp net corehtml action net corereturn action asp net corehow to call html action in asp net core net core 3 1 mvc alternative to html 2cacionasp ent core 3 0 does not contatin renderaction net core mvc 40html actionhow to call html action in asp net core