Asp.Net的mvc Url.Action外部JS文件?

编程入门 行业动态 更新时间:2024-10-24 20:16:20
本文介绍了Asp.Net的mvc Url.Action外部JS文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在外部js文件,我不能使用

In external js file, I cant use

url = "@Url.Action("Action", "Controller")" //url output : @Url.Action("Action", "Controller") //I get IllegalPath Name error.

当我写的是这样的:

url = "/Controller/Action"

和项目如果是子文件夹下,然后脚本不工作。我需要像这样的相对URL:

And If project is under a sub folder, then scripts do not work. I need something like this as relative Url:

url = "~/Controller/Action"

我怎么能这样做呢?谢谢你。

How can ı do this? Thanks.

推荐答案

由于.js文件不被asp mvc的视图引擎解析,你根本无法使用任何C#code在那里。我会建议使用不显眼的方式,像这样

As .js files are not parsed by asp mvc view engine, you simply cannot use any c# code in there. I would suggest using unobtrusive approach, something like this

<div id="loader" data-request-url="@Url.Action("Action", "Controller")"></div>

和在JavaScript中,数据请求URL的使用价值

And in javascript, use value of data-request-url

$(function(){ $('#loader').click(function(){ var url = $(this).data('request-url'); alert(url); }); });

更多推荐

Asp.Net的mvc Url.Action外部JS文件?

本文发布于:2023-11-03 20:36:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   mvc   Net   Asp   JS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!