在外部 JavaScript 中处理 ASP.NET MVC 路由
避免在 JavaScript 中对 URL 进行硬编码(主要在进行 AJAX 调用时使用)的最佳方法是什么?
过去:
- 使用
@Url.Action
或@Url.RouteUrl
的结果渲染 JavaScript 变量, - 传递
@Url.Action
或的结果>@Url.RouteUrl
到 init/ctor 中的 JavaScript。
有更好的办法吗?
最好做这样的事情:
var url = $.routes("actionName", "controllerName") // or "routeName" for named routes
$.post(url, { id = 1 }, function() { //.. });
这当然是不可能的(JavaScript 无法直接访问 ViewContext,因此无法访问路由表)。
但我想知道是否有一种方法可以为 JavaScript 设置我自己的“路由表”,只使用我知道需要的路由表? (例如我在视图中设置它)
人们如何处理这个问题?
What's the best way to avoid hardcoding URL's in JavaScript (primarily used when making AJAX calls)?
In the past:
- Render JavaScript variable with result of
@Url.Action
or@Url.RouteUrl
- Pass result of
@Url.Action
or@Url.RouteUrl
to JavaScript in init/ctor.
Is there a better way?
It would be good to do something like this:
var url = $.routes("actionName", "controllerName") // or "routeName" for named routes
$.post(url, { id = 1 }, function() { //.. });
Which of course isn't really possible (JavaScript doesn't have direct access the to the ViewContext and thus doesn't have access to the route tables).
But i'm wondering if there's a way i can kind of setup my own "route table" for JavaScript, with only the ones i know it would need? (e.g i set it up in the View)
How do people handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽管我更喜欢在视图中注入 javascript - 让 HTML 完成它的工作,而 javascript 完成它的工作。下面是图案。
对于链接
//Controller
这样两种类型的用户都可以处理启用 JavaScript 和禁用 JavaScript 的情况。
对于表格也可以这样做。
in-spite of injecting javascript in views i rather prefer - let HTML do its job and javascript do its. Below is the pattern.
For Links
//Controller
This way both type of users can be handled javascript enabled and javascript disabled.
Same can be done for forms.
实现 Javascript 路由引擎不会太困难。首先,将路由从 C# 序列化为 Javascript。其次,重新创建
Url.Action
方法。然而,对于我从事过的任何项目来说,这都有点过大了。我团队的项目始终呈现一个通用的 Javascript 变量,其中包含所有必要的 URL。
这种方法确保了强类型的操作方法,并且也更适合重构。
Implementing a Javascript routing engine wouldn't be too difficult. First, serialize the Routes from C# to Javascript. Second, recreate the
Url.Action
method.However, that's a bit overkill for any of the projects I've worked on. My team's projects have always rendered a common Javascript variable that holds all necessary URL's.
This approach ensures strongly-typed action methods and lends better to refactoring too.
这说起来容易做起来难,但是您的网站应该在关闭 JavaScript 的情况下完全正常运行。实现此目标后,您应该能够向您的网站添加 AJAX 支持,并重新使用锚标记中的现有 HREF 属性或 FORM 标记中的操作属性。该网站将更易于维护,因为您无需更新 JavaScript 文件中的链接。
This is easier said than achieved in practice, but your website should be fully functional with JavaScript turned off. When this is achieved, you should be able to add AJAX support to your website and re-use existing HREF attributes in your anchor tags or action attributes in your FORM tags. The website will be easier to maintain as you won't need to update links in your JavaScript files.
我决定实现我自己的 UrlFactory,在我的代码中直接使用 ASP.NET 帮助程序(Html/Url),现在我没有 src,明天我会发布它们。
优点:我可以轻松跟踪每个网址并以集中方式执行一些重写。
使用示例:
然后在 javascript 中使用 'em 和
一旦定义了自己的工厂,您就可以劫持“重要”url(例如用于重写),并为 Url 帮助器实现保留通用性。
然而,为了拥有这个完整的客户端,需要渲染 Js 路由上下文的额外步骤,以访问客户端变量。
编辑:正如我所承诺的,我非常简单的 Url 类生成器:
I've decided to implement my own UrlFactory, using ASP.NET helpers directly (Html/Url) in my code, now I don't have the src with me, I'll post'em tomorrow.
Pros on this: I can track each and every url easily and perform some rewriting in a centralized fashion.
Example of usage:
Then using 'em in javascript with
Once you've defined your own Factory, you can hijack "important" urls (eg. for rewriting), and leave common to the Url helper implementation.
However for having this fully client side, there's an extra step of rendering a Js routing context, for accessing client side variables.
EDIT: As promised my very simple Url class builder: