2014-03-07 97 views
0

請幫助... 我已經看到了這幾個職位,但他們似乎並沒有幫助...MVC 5路線不工作

我有一個路線定義時直接調用完美的作品; http:// localhost : 53505/GetLocationData/Canada/Ontario/Toronto 我得到了我期望的結果 - 來自我的AddressController。

...

現在,在我的申請,我ClientController通過客戶端/指數呼叫啓動一個視圖〜/查看/客戶/ Index.cshtml

這種觀點有一個.ajax的javascript試圖異步調用上面提到的相同的AddressController函數,從一個好的GEO-IP服務得到結果後: $(document).ready(function() {

var urlGeoIeoip = "http:// smart-ip . net/geoip-json?callback=?"; 

    $.ajax({ 
     url: urlGeoIeoip, 
     type: "GET", 
     dataType: "json", 
     timeout: 2000, 
     success: function (geoipdata) { 

     $("#AddressCity").data("kendoComboBox").value(geoipdata.city); 
     $("#AddressState").data("kendoComboBox").value(geoipdata.region); 
     $("#AddressCountry").data("kendoComboBox").value(geoipdata.countryName); 

     var $form = $(this); 

     $.ajax({ 
      url: "getlocationdata", 
      type: "GET", 
      data: { 'country': geoipdata.countryName, 'region': geoipdata.region, 'city': geoipdata.city }, 
      timeout: 500, 
      success: function(data) { 
       var $target = $($form.attr("data-htci-target")); 
       var $newHtml = $(data); 
       $target.replaceWith($newHtml); 
      } 
     }); 


     } 
    }).fail(function(xhr, status) { 
    if (status === "timeout") { 
     // log timeout here 
     } 
    }); 
}); 

它的工作原理,但請求得到我的ClientController而不是地址控制器的迴應!

我看到它進入/客戶端/索引與Request.Url是: http:// localhost : 53505/Client/Index/GetLocationData?country=Canada&region=Ontario&city=Toronto 爲什麼不到達我的AddressController?

這裏是我的RouteConfig:

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
     routes.IgnoreRoute("{resource}.ashx/{*pathInfo}"); 

     routes.MapRoute(
      name: "GetLocationData", 
      url: "getlocationdata/{country}/{region}/{city}", 
      defaults: new { controller = "Address", action = "GetLocationData"} 
     ); 

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
} 

回答

1

如果你改變內部$就鏈接到 「/ getlocationdata」(而不僅僅是 「getlocationdata」)?

+0

試過 - ajax調用在某個地方進入空間並被吸入黑洞。沒有控制器接收此Web應用程序的ajax調用。我不確定那個「/什麼?電話會在哪裏 –

+0

現在它似乎工作 - 也許有什麼緩存?現在好了 –

0

看起來你要發送通過查詢字符串中的數據(傳遞參數通過數據選項):

$.ajax({ 
     url: "getlocationdata", 
     type: "GET", 
     data: { 'country': geoipdata.countryName, 'region': geoipdata.region, 'city': geoipdata.city }, 
     timeout: 500, 
     success: function(data) { 
      var $target = $($form.attr("data-htci-target")); 
      var $newHtml = $(data); 
      $target.replaceWith($newHtml); 
     } 
    }); 

OPTION 1 正因爲如此,你應該這樣定義您的路線:

routes.MapRoute(
     name: "GetLocationData", 
     url: "getlocationdata", 
     defaults: new { controller = "Address", action = "GetLocationData"} 
    ); 

這偵聽路線:http://localhost:53505/GetLocationData?country=Canada&region=Ontario&city=Toronto

然後你可以使用UrlHelper,以便獲得url中的視圖(假設CSHTML下):

$.ajax({ 
     url: "@Url.RouteUrl("GetLocationData")", //Notice this is the Name of the Route 
     type: "GET", 
     data: { 'country': geoipdata.countryName, 'region': geoipdata.region, 'city': geoipdata.city }, 
     timeout: 500, 
     success: function(data) { 
      var $target = $($form.attr("data-htci-target")); 
      var $newHtml = $(data); 
      $target.replaceWith($newHtml); 
     } 
    }); 

這將讓你自由地改變你的路由配置的路由,而不必修改你的意見。

OPTION 2 如果你想保持路線像它(使用inurl這樣的數據),你將不得不這樣定義你的Ajax調用:

$.ajax({ 
     url: "/getlocationdata/" + geoipdata.countryName + "/" + geoipdata.region + "/" + geoipdata.city, //This will manually build the route as you defined it 
     type: "GET", 
     timeout: 500, 
     success: function(data) { 
      var $target = $($form.attr("data-htci-target")); 
      var $newHtml = $(data); 
      $target.replaceWith($newHtml); 
     } 
    }); 

你仍然可以使用UrlHelper爲這種類型的路線,但你不能在javascritpt中輕易使用它:

$.ajax({ 
     url: "@Url.RouteUrl("GetLocationData", new{country = "SomeCountry",region="SomeRegion",city="SomeCity"})", //unfortunately, you need these while you are in C# code 
     type: "GET", 
     timeout: 500, 
     success: function(data) { 
      var $target = $($form.attr("data-htci-target")); 
      var $newHtml = $(data); 
      $target.replaceWith($newHtml); 
     } 
    }); 
+0

感謝您的注意事項 - 它清除了一個不同的問題。 –