Account Service:
public async Task<string> PostRegistrationToApi(NameValueCollection form)
try
{
string str = await url.WithHeaders(new { Accept = "application /json", User_Agent = "Flurl" }).PostJsonAsync(myDictionary).ReceiveJson();
return str;
}
This is how it looks like in interface:
public interface IAccountService
{
Task<string> PostRegistrationToApi(NameValueCollection form);
This is my controller view:
string str = await _accountService.PostRegistrationToApi(myform);
return RedirectToAction("Register");
This is the error message I get:
Cannot implicitly convert type 'System.Dynamic.ExpandoObject' to 'string'
I could not figure out how to solve this error. Can someone tell me what this error message means and how to fix it? At first, I thought it's TResult type issue but seems like even if I change the variable to string type, consistent with TResult. I still get the same error. Thanks!
You're getting that error because ReceiveJson
returns a dynamic
, not a string. This allows you to work with the result as an object with properties without having to declare a matching class. This is handy for quick & dirty JSON responses where you know the shape of the response won't change, but you give up compile-time type checking, so in most cases I recommend creating a matching class and using RecieveJson<T>
.
If you really want to get a string back, you can use ReceiveString
instead. If you're just forwarding the response off to something else or dumping it to a log or something that can be useful. But if you need to somehow process the JSON result I'd recommend using ReceiveJson
or ReceiveJson<T>
so you can work with the result as a C# object.