MVC 5路由不起作用

时间:2014-03-07 16:22:09

标签: jquery asp.net-mvc asp.net-mvc-routing

请帮助...... 我已经看到了一些关于此的帖子,但它们似乎没有帮助......

我有一个定义的路线,直接调用时效果很好; http:// localhost : 53505/GetLocationData/Canada/Ontario/Toronto 而且我得到了我所期望的 - 来自我的AddressController的结果。

...

现在,在我的应用程序中,我的ClientController启动了一个视图〜/ Views / Client / Index.cshtml

通过调用客户/索引

该View有一个.ajax javascript,它尝试在从良好的GEO-IP服务获得结果后异步调用上面提到的相同AddressController函数: $(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
        }
    });
});

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而不是地址控制器的回答!!

我看到它进入/ Client / Index,Request.Url是: 为什么不到达我的AddressController?

这是我的RouteConfig:


    http:// localhost : 53505/Client/Index/GetLocationData?country=Canada&region=Ontario&city=Toronto

2 个答案:

答案 0 :(得分:1)

如果您将$ .ajax中的网址更改为“/ getlocationdata”(而不仅仅是“getlocationdata”),该怎么办?

答案 1 :(得分: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);
        }
    });

选项1 因此,您应该像这样定义您的路线:

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

这会侦听路线:http://localhost:53505/GetLocationData?country=Canada&region=Ontario&city=Toronto

然后您可以使用UrlHelper在视图中获取网址(假设下面是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);
        }
    });

这将允许您自由更改路线配置中的路线,而无需修改您的视图。

选项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);
        }
    });
相关问题