我是asp.net mvc项目的新手。我想从Home Controller中的ActionResult Index()方法调用另一个类中的方法。
例如:
public class RestCall{
public async Task<ActionResult> RestCall(string a, string b){
//......
}
}
我想从下面的类中的index方法访问RestCall()方法,我想将RestCall类的输出转换为home控制器中的index()方法。
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
(我使用的是razor语法。)有人可以建议吗?
答案 0 :(得分:1)
将Index()
的定义更改为异步并等待RestCall
:
public async Task<ActionResult> Index()
{
RestCall rc = new RestCall();
//rename method as @NightOwl88 says
var result = await rc.DoRestCall("somestring", "somestring").ConfigureAwait(false);
// do something with result
return View();
}