[英]Using @Url.Action in JS-asp.net MVC
I have a project that I have been creating and I publish it in the IIS, there everything is fine, I can visualize my Crystal Reports reports. 我有一个正在创建的项目,并将其发布在IIS中,一切正常,我可以可视化我的Crystal Reports报表。
Now I have re-published the project (update in IIS), but I can not see my Crystal Reports reports that I call from js. 现在,我已经重新发布了该项目(IIS中的更新),但是看不到从js调用的Crystal Reports报告。
var url = "/ReporteIngresoVP/RPVistaPreviaIngreso";
window.open(url, "_blank");
At the time of calling it, it opens a new tab (as it should be) and shows me a 404: 在调用它时,它会打开一个新选项卡(应该是),并显示404:
When I open another report (from the controller) it shows me the correct path
(everything from IIS): 当我打开另一个报告(从控制器)时,它向我显示了正确的
path
(所有来自IIS的path
):
As I read out there if I need to use urls that point to asp.net MVC elements from js, they should go with the @Url.Action
so I am trying the following: 正如我在这里读到的,如果我需要使用指向js中的asp.net MVC元素的URL,它们应该与
@Url.Action
一起使用,所以我尝试以下操作:
var url2='@Url.Action("RPVistaPreviaIngreso","CustomerReporteIngresoVP")';
window.open(url2, "_blank");
But when I navigate, I get the URL in this way: 但是,当我导航时,我是通过以下方式获取网址的:
How can I complete the route or can I go to the view/controller what I want in both case, Visual Studio and the project published using javascript? 在两种情况下(Visual Studio和使用JavaScript发布的项目),我如何才能完成路线,还是可以转到视图/控制器?
UPDATE UPDATE
I try to navigate from a click function, I never pass any url, what can I modify? 我尝试从点击功能进行导航,但我从未传递任何网址,该如何修改?
$('.vpBTN').click(function () {
//OTHER DATA
$.ajax({
url: "setDatosIngreso",
method: "POST",
data: {
factura: datoFactura,
poliza: datosPoliza,
fecha: datosFecha,
via: datosVia,
bruto: datosBruto,
neto: datosNeto,
bultos: datosBultos
},
async: false,
//dataType: "json",
success: function (respuesta) {
var url = "IKOR/ReporteIngresoVP/RPVistaPreviaIngreso";
window.open(url2, "_blank");
}
});
});
The problem is you're using external JS file which included inside Razor view, and obviously you cannot use Razor HTML helpers to render complete URL inside JS file. 问题是您正在使用包含在Razor视图中的外部JS文件,并且显然您不能使用Razor HTML帮助器在JS文件中呈现完整的URL。 You need to pass the URL as JS function parameter instead:
您需要改为将URL作为JS函数参数传递:
External JS 外部JS
// create this function to redirect
function redirectToUrl(url)
{
window.open(url, "_blank");
}
Razor view ( <script>
tag) 剃刀视图(
<script>
标签)
$('.vpBTN').click(function () {
// other data
$.ajax({
url: "setDatosIngreso",
method: "POST",
data: {
factura: datoFactura,
poliza: datosPoliza,
fecha: datosFecha,
via: datosVia,
bruto: datosBruto,
neto: datosNeto,
bultos: datosBultos
},
success: function (respuesta) {
var url = '@Url.Action("RPVistaPreviaIngreso","CustomerReporteIngresoVP")';
redirectToUrl(url);
}
});
});
What I did to solve it and can help someone: 我为解决该问题所做的工作并可以帮助某人:
Create a div
that has nothing, just with a clas
s and data-request-ur
l in this last I send my @Url.Action()
最后,我将
@Url.Action()
发送给一个创建没有任何内容的div
,只包含一个clas
和data-request-ur
l
<div class="urlPasar" data-request-url="@Url.Action("RPVistaPreviaIngreso","ReporteIngresoVP")"></div>
In JS capture the value of the data
that is in the div
在JS中捕获
div
data
的值
var url = $('.urlPasar').data('request-url');
window.open(url, "_blank");
Works for both in Visual Studio and in a published project 在Visual Studio和已发布项目中均适用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.