0

I'm working on Service which is generating dynamic Razor page connected with model, and the page needs to be sent to another service through HttpClient . This is the ActionResult which is generating the view:

// GET: /Details/
public async Task<IActionResult> DetailsSend(int id)
{
    if (id == null)
    {
        return NotFound();
    }
    
    var details = await _context.Details
        .FirstOrDefaultAsync(d=> d.Id == id);

    return View(details);
}

But i don't want to return this View(details). I want to send this dynamic view via httpclient. Any suggestion?

Rena
  • 30,832
  • 6
  • 37
  • 72
Rajzer
  • 1,174
  • 11
  • 24
  • Presumably you want to render the razor view to a string. There are lots of articles and posts about that (e.g. https://stackoverflow.com/q/483091/120955). Then you can send the result via HttpClient. – StriplingWarrior Jun 08 '21 at 17:44

2 Answers2

1

You could render the view as string like below:

public class EmployeesController : Controller
{
    private readonly YourDbContext _context;
    private readonly Microsoft.AspNetCore.Mvc.ViewEngines.ICompositeViewEngine _viewEngine;

    public EmployeesController(ICompositeViewEngine viewEngine, YourDbContext context)
    {
        _viewEngine = viewEngine;
        _context = context;
    }
    public async Task<string> DetailsSend(int id)
    {
        var details = await _context.Details
            .FirstOrDefaultAsync(d => d.Id == id);
        string view = "";
        ViewData.Model = details;
        using (var writer = new StringWriter())
        {
            ViewEngineResult viewResult = _viewEngine.FindView(ControllerContext, "Details", false);
            ViewContext viewContext = new ViewContext(
                                                        ControllerContext,
                                                        viewResult.View,
                                                        ViewData,
                                                        TempData,
                                                        writer,
                                                        new HtmlHelperOptions()
                                                        );

            await viewResult.View.RenderAsync(viewContext);
            view = writer.GetStringBuilder().ToString();
        }

        return view;
    }
}

Then you could use HttpClient to call this api and get the result like below:

public async Task<IActionResult> Index()
{
    var url = "https://localhost:portNumber/ControllerName/detailssend/1";//it should be the url of your api
    var httpClient = new HttpClient();
    var response = await httpClient.GetAsync(url);
    using (var content = response.Content)
    {
        //get the json result from your api
        var result = await content.ReadAsStringAsync();
    }
}
Rena
  • 30,832
  • 6
  • 37
  • 72
  • can you describe how you create IHttpActionResult for the url ``` "https://localhost:portNumber/ControllerName/detailssend/1" ``` or something or you direct send from async Task? – Rajzer Jun 10 '21 at 07:29
  • Hi @Rajzer, not sure what do you mean.Maybe could you please explain more for me? It is just the common way about how to use httpclient to call api. – Rena Jun 10 '21 at 07:35
  • And I think what is the importance should be how to generate view to dynamic string in your scenario. – Rena Jun 10 '21 at 07:37
-3

You should write as follows:

 var details = (await context (d => d.Id == id)).Details.FirstOrDefaultAsync();
Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33
Saeed
  • 1
  • 1