没有静态控制器URL(魔术字符串)的ASP.NET MVC AJAX调用 [英] ASP.NET MVC AJAX calls without static Controller URLs (magic strings)

查看:59
本文介绍了没有静态控制器URL(魔术字符串)的ASP.NET MVC AJAX调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

有什么清晰,安全,系统的方法可以从JavaScript调用我的MVC控制器/动作,但是将相关的URL放在一个中央位置,以便在控制器更改时进行更新?还是要在运行时动态生成URL?还是要获取AJAX控制器链接的编译时检查?

What is a clear, safe and systematic way to call my MVC controllers/actions from JavaScript, but keep the related URLs in a central place for updating when controllers change? Or to dynamically generate the URLs at run-time? Or to get compile-time checking for AJAX controller linkage?

研究/过去尝试:

我最近将T4MVC插入了我的MVC项目.伟大的!在我的视图中进行控制器链接的编译时检查-但是我的JavaScript上仍然乱七八糟地出现了控制器URL.更糟糕的是,我已经在整个项目生命周期中进行了改进,因此我以多种方式进行工作,而这些方式我仍然不满意.

I've recently plugged in T4MVC to my MVC project. Great! Compile-time checking for controller linkage in my views - but I still have controller URLs littered all over my JavaScript. Worse, I have evolved how I do it over the project lifetime, so I do it multiple ways that I'm still not satisfied with.

1)一种方式:

locationAutoComplete = new AjaxQueue({
 Controller: "Utilities", // null for current controller
 Action: "GetMatchingLocations",
 DataModel: null, // null for raw JSON, or a local model to populate
 MaxQueuedRequests: 0
});


2)另一种方式:


2) Another way:

locationAutoComplete = function (location) {
 return $.ajaxPost(resolveUrl("~/Utilities/GetMatchingLocations"));
}


3)上面的1和2都使用了在Master/Layout JavaScript代码段中定义的项目根目录,但仍使用未经检查的URL字符串.因此,现在我开始通过从我的.CSHTML视图中定义控制器字符串开始从T4MVC中受益:


3) Both 1 and 2 above make use of a project root defined in a Master/Layout JavaScript snippet, but still use unchecked URL strings. So now I've started benefiting from T4MVC by simply defining controller strings in my .CSHTML view:

var locationLookupController = @Url.Action(MVC.Utilities.GetMatchingLocations());


4)SO文章( Ajax调用到MVC Controller- Url Issue )来解决这个问题.它讨论了方法3,但也建议使用AjaxHelper命名空间.由于我大量使用jQuery AJAX进行了一些自定义的交互(例如,将结果加载到本地数据模型中,而不是在单击事件中而是在一定的时间间隔内),所以我认为这不会对我有多大帮助.今天,我什至没有在我的任何页面上都包含MS AJAX JavaScript代码.


4) The SO article ( Ajax call Into MVC Controller- Url Issue ) addresses this a little. It talks about method 3, but also suggests using the AjaxHelper namespace. Since I make heavy use of jQuery AJAX with some pretty customized interactions (e.g. loading results into a local data model, not at a click event but at a timed interval), I don't think it will help me much. Today I don't even include the MS AJAX JavaScript code on any of my pages.

谢谢!

推荐答案

继续使用T4MVC.混合使用一些jQuery和HTML5 data- *属性.这是一个示例:

Continue to use T4MVC. Mix it in with a little jQuery and HTML5 data-* attributes. Here's an example:

<div data-location-lookup-url="@Url.Action(MVC.Utilities.GetMatchingLocations())">

...

locationAutoComplete = function (location) {
    var url = $('[data-location-lookup-url]').data('location-lookup-url');
    return $.ajaxPost(url);
}

这篇关于没有静态控制器URL(魔术字符串)的ASP.NET MVC AJAX调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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