ASP.NET MVC和jQuery-路由和脚本位置 [英] ASP.NET MVC and jQuery - routes and script locations
问题描述
我正在ASP.NET MVC中使用概念验证应用程序,并且在使用jQuery时遇到有关路线的查询.我的功能之一要求我调用〜/jqdata/myFunction/,使用
I am playing with a proof-of-concept app in ASP.NET MVC and I have a query about routes when using jQuery. One of my functions requires me to call ~/jqdata/myFunction/ and this is all fine using
$.get("/jqdata/myFunction/" + $(this).attr("name"), function(data) { alert (data); }
但是,我将无法控制将应用程序部署到的虚拟目录.在Site.Master页面中,我可以判断样式和js文件的行为,例如,其头部为runat ="server",并具有指向~~/Styles/site.css的链接,但我不知道该如何做.确保jQuery调用正确的路径来获取其数据.
However, I am not going to be able to control the virtual directotry into which the app is to be deployed. Within the Site.Master page, I can cludge this behaviour for styles and js files with runat="server" in the head, and a link to"~/Styles/site.css", for example, but I have no idea how to ensure jQuery calls the correct path to get its data.
任何人都可以提供帮助,即使这意味着称呼我做这种事情的白痴或缺少明显的东西吗?我是一个winforms家伙,真的,你知道. :)
Can anyone help, even if it means calling me an idiot for doing it this way or missing something really obvious? I'm a winforms guy, really, you see. :)
谢谢 马克
推荐答案
您可以使用服务器端代码将网站的根设置为JavaScript中的变量,可能是在MasterPage中,也可能只是在视图中需要它用于:
You could use server side code to set the root of your website as a variable in JavaScript, perhaps in your MasterPage or maybe just in the view(s) you need it for:
var rootUrl = '<%= Html.ResolveUrl("~/")%>';
然后您的功能将类似于:
Then your function would be something along the lines of:
$.get(rootUrl + "jqdata/myFunction/" ...);
这篇关于ASP.NET MVC和jQuery-路由和脚本位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!