If you have to interact with such a "legacy" app, as you mentioned in your comment, the first step is to analyze what request is sent to the application, ones the form is submitted. Usually, you could "guess" it is based on the name of the form inputs. However, sometimes javascript interfere, so the most promising way is to submit the form and use the network tab of the browser's developer tools (F12). Personally, I like the way Chrome (and Edge) display the request, but Firefox hasn't excellent support too.
I like to preserve the request during request while analyzing. You should see the Request URL, the method, and the date.
You said it is a GET request. Unusual for forms but possible. In this case, the parameters are appended to the URL, and the debug output should look similar to this.
Equipped with these insights, we can jump to the Blazor side of things. Let's assume the following model is used inside the blazor application and in the legacy application.
public class ItemModel
{
public String Name { get; set; }
public String Description { get; set; }
}
HttpPost with form data
public async Task SendFormData(ItemModel input)
{
HttpClient client = _clientFactory.CreateClient();
String url = "<URL of your legacy app method from the network tab>";
var values = new Dictionary<String, String>
{
{ nameof(ItemModel.Name), input.Name },
{ nameof(ItemModel.Description), input.Description },
};
var requestContent = new FormUrlEncodedContent(values);
var response = await client.PostAsync(url, requestContent);
}
GET with URL appending
public async Task SendDataAsGet(ItemModel input)
{
HttpClient client = _clientFactory.CreateClient();
String url = "<URL of your legacy app method from the network tab>";
String urlWithValues = $"{url}?{nameof(ItemModel.Description)}={input.Name}&{nameof(ItemModel.Name)}={input.Description}";
var response = await client.GetAsync(urlt);
}
Remember that your model in the blazor application can be different from those you send to the legacy app. You could also apply any transformation if necessary. So you could substitute things like nameof(ItemModel.Name)
with NameOfLegacyProperty
to make it work.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…