Search code examples
asp.net-mvcasp.net-core-mvc

ASP.NET Core 7 MVC - How to call a controller and get the associated view from another MVC app


We have several MVC applications that do different jobs for different clients. For example we have a MVC app that is used for submitting billing packets to a billing client.

We converting an existing application to MVC, this application needs to integrate with the billing application so we can utilize the controllers and views from the billing application in our MVC conversion application.

We really don't want to re-develop these pages and controllers or have multiple copies so if we change one we have to remember to change all.

I looked at AddApplicationPart service, unless I am misunderstanding how this works it looks like any time the billing application changes we would have to re-compile and re-deploy our main app.

I have also looked at Redirect() but I don't see a way I can pass parameter values using this method.

Is there a way I can call a controller on the billing application and get the resulting view back to my calling application?

I have tried using HttpClient, with no luck.

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");

    string baseAddress = string.Empty;

    // set base address
    if (baseAddress != string.Empty)
    {
        HttpResponseMessage response = client.PostAsJsonAsync($"{baseAddress}Home/Login/", model).Result;

        if (response.IsSuccessStatusCode)
        {
            return response.Content.ReadFromJsonAsync<IActionResult>().Result;
        }
    }
}

The above code calls the correct controller in the target application, however the model is completely empty. I have confirmed that at the time of the call the model is populated.

If I add the [HttpPost] tag to the Login function, then the above code returns a not found error.

Any guidance is greatly appreciated.


Solution

  • Seems you want your current app work as gateway?

    you should read response content as Byte array /stream and retrun a html file for your requirement

    A minimal example:

    public async Task<IActionResult> ExistPage()
    {
        using var httpclient=new HttpClient() { BaseAddress= ....... };            
        var response = await httpclient.GetAsync("/Home/Index");
        // check if success here yourself
        var  responsestream = await response.Content.ReadAsStreamAsync();
        return File(responsestream, "text/html");
    }
    

    Result: enter image description here

    The above code calls the correct controller in the target application, however the model is completely empty

    Since you send the model as json with the codes:client.PostAsJsonAsync($"{baseAddress}Home/Login/", model),you have check the controller of another App,if the parameter is aatached with [FromBody] attribute

    Please check this document related with model-binding

    and the codes that how to send formdata with httpclient:

    using var httpclient=new HttpClient() { BaseAddress= ..... };
    var requestmessage = new HttpRequestMessage(HttpMethod.Post,"yourrequestpath");
    var formData = new MultipartFormDataContent();
    formData.Add(new StringContent("val"), "key");
    ....//add key -value pairs one by one
    requestmessage.Content = formData;
    var response = await httpclient.SendAsync(requestmessage);