建立了相对网址,包含JavaScript的MVC应用程序 [英] building relative URLs for an MVC app with JavaScript

查看:78
本文介绍了建立了相对网址,包含JavaScript的MVC应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了麻烦C#和JavaScript / jQuery来这里发挥好。

I'm having trouble getting C# and JavaScript/jQuery to play nice here.

我有一个淘汰赛视图模型,普通的旧JavaScript对象......它的属性/方法之一触发了一个阿贾克斯()通话和URL参数使用它的一些其他的属性值(JavaScript的变量)构建的。

I have a knockout view model, plain old javascript object... one of its property/methods fires off an .ajax() call, and the url parameter is built using some of its other property values (javascript variables).

在JavaScript中完全包含,但是当部署为一个应用程序来IIS,相对路径是大清洗这工作得很好。

This works fine when completely contained in JavaScript, but when deployed as an app to IIS, the relative pathing is hosed.

在MVC3通常我会使用类似 @ Url.Action ,让服务器端建立的地址...但同样,诀窍是C#不知道的改变JavaScript的值。

In MVC3 normally I would use something like @Url.Action and let the server side build the address... but again, the trick is C# is unaware of the changing javascript values.

var viewModel = {
    vendors: ko.observableArray([]),
    count: ko.observable(10),
    page: ko.observable(1),
    filterText: ko.observable(""),
    submit: function () {
        $.ajax({
            // works fine, until deploy when it is no longer a site relative URL
            url: 'vendors/' + viewModel.count() + '/' + viewModel.filterText(),

            // does not work, because C# is unaware of the javascript variables.
            //url: @Url.Action("Vendors", "Home", new { count = viewModel.count(), filter = viewModel.filterText() })

            dataType: 'json',
            success: function (data) {
                viewModel.vendors(data);
            }
        });    
    }
    // next: // load sequence starting with (page+1 * count) 
    // previous: // load sequence starting with (page-1 * count)
};
ko.applyBindings(viewModel);


问:

我的问题然后,我怎么能建立一个使用JavaScript变量值的Ajax调用的网址(例如计数,filterText),并仍然从应用程序?


Question:

My question then is, how can I build the url for the ajax call using the javascript variable values (ex. count, filterText) and still map from the relative root of the application?

推荐答案

一种可能是把那些JavaScript的值作为请求参数:

One possibility is to send those javascript values as request parameters:

$.ajax({
    url: '@Url.Action("vendors")',
    data: { count: viewModel.count(), filter: viewModel.filterText() },
    dataType: 'json',
    success: function (data) {
        viewModel.vendors(data);
    }
});

当然,这意味着你正在使用缺省路由参数会简单地发送到服务器无论是作为查询字符串参数(如果您使用的是GET)或POST请求身体的一部分。在这两种情况下,你会获取它们在服务器上以同样的方式:

Of course this implies that you are using default routes and the parameters will simply be sent to the server either as query string parameters (if you are using GET) or as part of the POST request body. In both cases you will fetch them on the server the same way:

public ActionResult Vendors(int count, string filter)
{
    ...
}

另一种可能性,如果你绝对坚持要把你的AJAX请求一些定制的路线,是使用一个简单的字符串替换:

Another possibility, if you absolutely insist on having some custom routes for your AJAX requests, would be to use a simple string replace:

var url = '@Url.Action("vendors", new { count = "__count__", filter = "__filterText__" })';
url = url.replace('__count__', viewModel.count())
         .replace('__filter__', viewModel.filterText());
$.ajax({
    url: url,
    dataType: 'json',
    success: function (data) {
        viewModel.vendors(data);
    }
});

这篇关于建立了相对网址,包含JavaScript的MVC应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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