【问题标题】:How to achieve the below code in asp.net2.0?如何在asp.net2.0中实现如下代码?
【发布时间】:2013-04-02 05:37:16
【问题描述】:

由于我在使用以下类时使用的是 asp.net2.0,因此出现以下错误。

Error 1 : The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

我用过的类。如何在asp.net2.0中使用这段代码而不会出错

public static XElement GetGeocodingSearchResults(string address)
{
    // Use the Google Geocoding service to get information about the user-entered address
    // See http://code.google.com/apis/maps/documentation/geocoding/index.html for more info...
    var url = String.Format("http://maps.google.com/maps/api/geocode/xml?  
    address={0}&sensor=false", HttpContext.Current.Server.UrlEncode(address));

    // Load the XML into an XElement object (whee, LINQ to XML!)
    var results = XElement.Load(url);

    // Check the status
    var status =results.Element ("status").Value;
    if (status != "OK" && status != "ZERO_RESULTS")
        // Whoops, something else was wrong with the request...
        throw new ApplicationException("There was an error with Google's Geocoding Service: " + status);

    return results;
}

【问题讨论】:

    标签: c# asp.net-2.0


    【解决方案1】:

    var 只是右侧表达式的实际类型的shortcut

    public static XElement GetGeocodingSearchResults(string address)
    {
        // Use the Google Geocoding service to get information about the user-entered address
        // See http://code.google.com/apis/maps/documentation/geocoding/index.html for more info...
        string url = String.Format("http://maps.google.com/maps/api/geocode/xml?address={0}&sensor=false",
           HttpContext.Current.Server.UrlEncode(address));
    
        // Load the XML into an XElement object (whee, LINQ to XML!)
        XElement results = XElement.Load(url);
    
        // Check the status
        string status =results.Element ("status").Value;
        if (status != "OK" && status != "ZERO_RESULTS")
            // Whoops, something else was wrong with the request...
            throw new ApplicationException("There was an error with Google's Geocoding Service: " + status);
    
        return results;
    }
    

    但是 LINQ to XML(以及整个 LINQ 功能)仅在 .NET 3.5 及更高版本中可用。您应该升级到 .NET 3.5 或 switch to System.Xml

    【讨论】:

    • 我试过了,但我在XElement.Load(url)得到以下错误。错误如下Cannot implicitly convert type 'System.Xml.Linq.XElement' to 'string'. An explicit conversion exists (are you missing a cast?)
    • 如果您能帮助我以 System.Xml 格式编写上述代码,对我将非常有帮助
    • 确保你有XElement results = XElement.Load(url);,而不是string results = XElement.Load(url);
    • 您评论中的错误表明您已安装.NET 3.5。因此,您不必切换到System.Xml。请使用您目前拥有的确切代码更新问题。
    • 是的,你们都是对的。我在Web.config 中放了一些代码,以使编译器在3.5 中运行。代码如下<system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4"> <providerOption name="CompilerVersion" value="v3.5"/> <providerOption name="WarnAsError" value="false"/> </compiler> </compilers> </system.codedom>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 2021-12-11
    相关资源
    最近更新 更多