jquery如何在我使用MVC4的Get方法上找到正确的url
我基本上是在mvc4 razor页面中使用Jquery,因此我可以像这样使用url.actionjquery如何在我使用MVC4的Get方法上找到正确的url,jquery,asp.net-mvc-4,get,Jquery,Asp.net Mvc 4,Get,我基本上是在mvc4 razor页面中使用Jquery,因此我可以像这样使用url.action $.get('@Url.Action("details","user")' $.get('~/Controllers/user/details') $.get('~/user/details') 我现在已经将jquery移动到了一个外部文件中,显然我无法使用razor助手获得正确的URL,我如何从外部js页面获得相同的URL,我尝试了一些新方法,但它不是这样工作的 $.get('@Url.Ac
$.get('@Url.Action("details","user")'
$.get('~/Controllers/user/details')
$.get('~/user/details')
我现在已经将jquery移动到了一个外部文件中,显然我无法使用razor助手获得正确的URL,我如何从外部js页面获得相同的URL,我尝试了一些新方法,但它不是这样工作的
$.get('@Url.Action("details","user")'
$.get('~/Controllers/user/details')
$.get('~/user/details')
我想知道如何在不使用全局变量的情况下找到它,我见过许多网站像这样使用它
$.get('details.html')
但是,由于这是mvc,不起作用,任何帮助都将是巨大的
这些路线是标准的MVC路线
routes.MapRoute(
name: "profile", // Route name
url: "{controller}/{action}", // URL with parameters
defaults: new { controller = "profile", action = "detail", id = UrlParameter.Optional }
);
根据您的路线(请参见Global.asax
),您需要提供绝对url,例如http://yoursite1.com/user/details
由于依赖性,这不是一个好的做法。如果网站/路线发生更改,则必须更改js文件
最佳实践
您需要做的是在外部文件中定义一个函数,并在视图中调用它,如下所示
在your view.cshtml中
<script src='script.js'> </script>
<script>
doByGet('@Url.Action("details","user")');
</script>
我们在应用程序中就是这样做的(我知道你说没有全局变量,但我觉得这个想法非常可行) 外部文件(.js) 在CSHTML中
<script>
baseUrl = '@Url.Action("PlaceHolder", new { area = "YourArea", controller = "YourController"})';
baseUrl = baseUrl.replace('PlaceHolder',''); //Remove placeholder from baseUrl, alternatively you may place blank in first statement
</script>
baseUrl='@Url.Action(“占位符”,新的{area=“yourrea”,controller=“YourController”});
baseUrl=baseUrl.replace('PlaceHolder','')//从baseUrl中删除占位符,或者您可以在第一条语句中空白
您能提供您的路由配置吗?是的,只需将上述路由放入新的编辑中即可。很荣幸(y)!