不能从异步方法针对返回值 [英] cannot return value from async method in view

查看:138
本文介绍了不能从异步方法针对返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图返回从值异步 HTML辅助,但它给下面的字符串,而不是期望的值。


  

System.Threading.Tasks.Task + WhenAllPromise`1 [System.Decimal]


方法:

 公共异步静态任务<&小数GT; CalculateCurrency(这的HtmlHelper帮手,小数金额,从字符串,字符串国家)
    {
        如果(的await的getValue(国家))
        {
            字符串fromCurrency = string.IsNullOrEmpty(从)? USD:从;
            字符串toCurrency =国家;
            Web客户端的客户端=新的WebClient();
            字符串的URL =的String.Format(http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={0}{1}=X,fromCurrency.ToUpperInvariant() ,toCurrency.ToUpperInvariant());
            流响应=等待client.OpenReadTaskAsync(URL);
            StreamReader的读者=新的StreamReader(响应);
            字符串yahooResponse =等待reader.ReadLineAsync();
            response.Close();
            如果(!string.IsNullOrWhiteSpace(yahooResponse))
            {
                串[]值= Regex.Split(yahooResponse,,);
                如果(values​​.Length大于0)
                {
                    小数率= System.Convert.ToDecimal(值[1]);
                    字符串资源=的String.Format({0:0.00},率*量);
                    返回decimal.Parse(RES);
                 }
            }
            返回decimal.Zero;
        }
        返回decimal.Zero;
    }

通话HTML助手:

  @ Html.CalculateCurrency(22,USD,EUR)


解决方案

视图不支持异步方法。因此,作为结果,你会得到默认的结果的ToString()的结果类型,确实返回只需键入名称的功能。

选项:


  • 将code到控制器和异步顶层(非独生子女)行动调用等待。通过数据模型通过查看或 ViewBag

  • 转化为真正的同步code,如果你必须从视图或儿童的行动称之为

  • 如果无法尝试。结果,但要注意死锁。请参见等待VS Task.Wait - ?的死锁的详细信息/链接

注:移动异步 code到孩子的行动不会帮助

I'm trying to return value from async html helper but it is giving following string instead of desired values.

"System.Threading.Tasks.Task+WhenAllPromise`1[System.Decimal]"

Method:

public async static Task<decimal> CalculateCurrency(this HtmlHelper helper, decimal amount, string from, string country)
    {
        if (await getValue(country))
        {
            string fromCurrency = string.IsNullOrEmpty(from) ? "USD" : from;
            string toCurrency = country;
            WebClient client = new WebClient();
            string url = string.Format("http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={0}{1}=X", fromCurrency.ToUpperInvariant(), toCurrency.ToUpperInvariant());
            Stream response = await client.OpenReadTaskAsync(url);
            StreamReader reader = new StreamReader(response);
            string yahooResponse = await reader.ReadLineAsync();
            response.Close();
            if (!string.IsNullOrWhiteSpace(yahooResponse))
            {
                string[] values = Regex.Split(yahooResponse, ",");
                if (values.Length > 0)
                {
                    decimal rate = System.Convert.ToDecimal(values[1]);
                    string res = string.Format("{0:0.00}", rate * amount);
                    return decimal.Parse(res);
                 }
            }
            return decimal.Zero;
        }
        return decimal.Zero;
    }

Call HTML Helper:

@Html.CalculateCurrency(22, "USD", "EUR")

解决方案

Views don't support asynchronous methods. So as result you get result of default .ToString() function for type of result which indeed returns just type name.

Options:

  • move code to controller and call in from asynchronous top level (non-child) action with await. Pass data to view via model or ViewBag
  • convert to real synchronous code if you must call it from the view or child action
  • if not possible try .Result, but watch out for deadlocks. See await vs Task.Wait - Deadlock? for details/links.

Note: moving async code to child action will not help.

这篇关于不能从异步方法针对返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆