簡體   English   中英

在ASP.NET中的MVC 4中添加asmx文件

[英]adding asmx file in MVC 4 in ASP.NET

我在Visual Studio 2013的ASP.NET中有一個MVC項目,並且我有一個asmx文件,其中包含一個函數,該函數包含對Google距離矩陣的調用

在索引頁面中,我需要調用asmx文件,

asmx文件包含以下功能:

public double GetDrivingDistanceInMiles(string origin, string destination)
    {
        string url = @"http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" +
          origin + "&destinations=" + destination +
          "&mode=driving&sensor=false&language=en-EN&units=imperial";
        //create object of HttpWebRequest that create requested object for the given url
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        WebResponse response = request.GetResponse();
        Stream dataStream = response.GetResponseStream();
        StreamReader sreader = new StreamReader(dataStream);
        string responsereader = sreader.ReadToEnd();
        response.Close();

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(responsereader);


        if (xmldoc.GetElementsByTagName("status")[0].ChildNodes[0].InnerText == "OK")
        {
            XmlNodeList distance = xmldoc.GetElementsByTagName("distance");
            return Convert.ToDouble(distance[0].ChildNodes[1].InnerText.Replace(" mi", ""));
        }

        return 0;
    }

你能告訴我如何在MVC解決方案中添加asmx文件並調用它嗎

或如何在索引文件中添加此函數,我無法在控制器中添加它,因為我需要在這樣的循環中調用它

<% foreach (var item in Model) { %>

我試圖將代碼放在這樣的Models類中:

Google服務類

距離等級

在“索引”頁面中,我這樣稱呼它:

<% foreach (var item in Model) { %>
         <%: Html.DisplayFor(item.DrivingDistanceInMiles) %>

您正在以錯誤的方式思考。 您既不想“添加並調用asmx”,也不想“在索引文件中添加此功能”。

既然有了代碼,只需將代碼本身作為服務類添加到項目中的某個位置。 像這樣簡單:

public class MapService
{
    public double GetDrivingDistanceInMiles(string origin, string destination)
    {
        // your code...
    }
}

理想情況下,您希望此服務實現一個接口(類似於IMapService ),然后將該接口作為依賴項注入到代碼中。它可能超出了您現在想做的工作,但是一些東西。)

然后,您可以在項目中的任何地方創建該服務的實例並使用它。 鑒於您要在此處執行的操作:

foreach (var item in Model) {

看來您的視圖模型是一個集合,並且您想對該集合中的每個項目計算此值。 一種方法可能是將計算出的屬性添加到模型本身。 像這樣:

// in your view model class
public double DrivingDistanceInMiles
{
    get
    {
        return new MapService().GetDrivingDistanceInMiles(this.Origin, this.Destination);
    }
}

請注意,當然,我猜測的是模型的結構及其可調用的屬性。 但是想法是一樣的。 您可以將服務方法設為static以避免實例化新的服務對象。 或者,你甚至可以把GetDrivingDistanceInMiles()直接在視圖模型類,如果這是在你的項目,該項目會需要它的唯一的類。 (不過,如果您確實采用了依賴項注入路線,由於Google Maps API是外部依賴項,並且不屬於模型,所以我建議將其作為單獨的服務保留。)

然后,在您看來,您只需綁定到該屬性:

foreach (var item in Model) {
    // display item.DrivingDistanceInMiles somehow
}

您可以采用多種方法來構造它。 但是貫穿以下幾點:

  1. 不要“添加asmx”。 ASMX是一種應用程序宿主技術。 當您想要做的只是調用代碼本身時,就不必在代碼后面隱藏代碼了。 如果要多個項目共享此代碼(一個MVC,一個ASMX),則只需將代碼本身放在兩個項目引用的單獨的類庫中。
  2. 不要“在索引文件中添加此功能”。 視圖僅綁定到模型,它們不應包含復雜的服務器端代碼。 這些模型將引用它們生成視圖綁定到的值所需的任何服務。

謝謝大衛,您啟發我通過創建一個名為“ CallGoogleService”的新控制器來解析代碼,我在其中輸入了函數:

public double GetDrivingDistanceInMiles(string origin, string destination)
    {
        string url = @"http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" +
      origin + "&destinations=" + destination +
      "&mode=driving&sensor=false&language=en-EN&units=imperial";
        //create object of HttpWebRequest that create requested object for the given url
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        WebResponse response = request.GetResponse();
        Stream dataStream = response.GetResponseStream();
        StreamReader sreader = new StreamReader(dataStream);
        string responsereader = sreader.ReadToEnd();
        response.Close();

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(responsereader);


        if (xmldoc.GetElementsByTagName("status")[0].ChildNodes[0].InnerText == "OK")
        {
            XmlNodeList distance = xmldoc.GetElementsByTagName("distance");
            return Convert.ToDouble(distance[0].ChildNodes[1].InnerText.Replace(" mi", ""));
        }

        return 0;
    }

然后在索引視圖中,我將這段代碼稱為

<% MvcNursery.Controllers.CallGoogleService ob = new MvcNursery.Controllers.CallGoogleService();%>

<% foreach (var item in Model) { %>
 <%string d =ob.GetDrivingDistanceInMiles(Postcode1,Postcode2).ToString(); %>

我知道有一個更簡單的方法來調用此控制器,但是不幸的是我確實知道如何。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM