Asp。净Mvc Url。在外部js文件中操作?

时间:2022-11-30 21:13:19

In external js file, I cant use

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

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

When I write like this:

当我这样写:

url = "/Controller/Action"

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

如果项目在子文件夹下,那么脚本就不起作用了。我需要这样的相对Url:

url = "~/Controller/Action"

How can ı do this? Thanks.

ı怎么能这样做呢?谢谢。

3 个解决方案

#1


116  

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

由于.js文件没有被asp.net mvc视图引擎解析,因此您不能在其中使用任何c#代码。我建议使用不引人注目的方法,像这样。

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

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

在javascript中,使用数据请求-url的值

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

#2


6  

I'm not sure if this is the most elegant solution, but what I did was differentiating between registers and the real implementation in the external scripts, so that:

我不确定这是否是最优雅的解决方案,但我所做的是区分寄存器和外部脚本中的实际实现,因此:

<script>...</script>
... include all the external scripts I need

$(document).ready(function(){

    //get all the information you need from your MVC context 
    //before going out of context and into the scripts
    var url = '@Url.Action("Action", "Controller")'; 


     RegisterMyFunction(url, other parameters ..);
     RegisterAnotherFunction(url, others...);
}

So that in my views I only had the register functions and the scripts contained the special values as a parameter to do whatever I wanted.

所以在我的视图中,我只有寄存器函数脚本包含特殊值作为参数来做我想做的事情。

Hope it helps,

希望它可以帮助,

#3


2  

Here's a pattern I've been using. It's a bit more steps, but I like that all of my urls are in one organized location in the View.

这是我一直在使用的一种模式。这需要更多的步骤,但是我喜欢所有的url都在视图的一个有组织的位置。

At the bottom of my View I will include a Scripts Section that contains the urls like so:

在我的视图底部,我将包含一个脚本部分,其中包含如下url:

@section Scripts
{
    <script type="text/javascript">
        myJavaScriptObject.firstUrl = '@Url.Action("Action1", "Controller", new {id = Model.Id})';
        myJavaScriptObject.secondUrl = '@Url.Action("Action2", "Controller", new {id = Model.Id})';
    </script>
}

Inside of my JavaScript Class (which is in an external file) I will reference the url like so:

在我的JavaScript类(在一个外部文件中)中,我将这样引用url:

var myJavaScriptObject = {
    firstUrl: '',
    secondUrl: '',
    docReady: function() {
        $.get(myJavaScriptObject.firstUrl, function(data) { do something...  });
    }
}

I realize the entries don't need to be referenced inside of the Class, but I like to have them there for my own housekeeping.

我意识到这些条目不需要在类中引用,但是我喜欢把它们放在那里作为我自己的管理。

#1


116  

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

由于.js文件没有被asp.net mvc视图引擎解析,因此您不能在其中使用任何c#代码。我建议使用不引人注目的方法,像这样。

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

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

在javascript中,使用数据请求-url的值

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

#2


6  

I'm not sure if this is the most elegant solution, but what I did was differentiating between registers and the real implementation in the external scripts, so that:

我不确定这是否是最优雅的解决方案,但我所做的是区分寄存器和外部脚本中的实际实现,因此:

<script>...</script>
... include all the external scripts I need

$(document).ready(function(){

    //get all the information you need from your MVC context 
    //before going out of context and into the scripts
    var url = '@Url.Action("Action", "Controller")'; 


     RegisterMyFunction(url, other parameters ..);
     RegisterAnotherFunction(url, others...);
}

So that in my views I only had the register functions and the scripts contained the special values as a parameter to do whatever I wanted.

所以在我的视图中,我只有寄存器函数脚本包含特殊值作为参数来做我想做的事情。

Hope it helps,

希望它可以帮助,

#3


2  

Here's a pattern I've been using. It's a bit more steps, but I like that all of my urls are in one organized location in the View.

这是我一直在使用的一种模式。这需要更多的步骤,但是我喜欢所有的url都在视图的一个有组织的位置。

At the bottom of my View I will include a Scripts Section that contains the urls like so:

在我的视图底部,我将包含一个脚本部分,其中包含如下url:

@section Scripts
{
    <script type="text/javascript">
        myJavaScriptObject.firstUrl = '@Url.Action("Action1", "Controller", new {id = Model.Id})';
        myJavaScriptObject.secondUrl = '@Url.Action("Action2", "Controller", new {id = Model.Id})';
    </script>
}

Inside of my JavaScript Class (which is in an external file) I will reference the url like so:

在我的JavaScript类(在一个外部文件中)中,我将这样引用url:

var myJavaScriptObject = {
    firstUrl: '',
    secondUrl: '',
    docReady: function() {
        $.get(myJavaScriptObject.firstUrl, function(data) { do something...  });
    }
}

I realize the entries don't need to be referenced inside of the Class, but I like to have them there for my own housekeeping.

我意识到这些条目不需要在类中引用,但是我喜欢把它们放在那里作为我自己的管理。