ASP。净MVC Url。无法识别JQuery中的操作

时间:2022-06-01 12:50:03

I am trying to use Url.Action() method in my js file to define urls for my ajax calls. So far I have failed.

我正在尝试在js文件中使用Url.Action()方法来定义ajax调用的url。到目前为止,我失败了。

 $.ajax(
 {
    type: "POST",
    url: '@Url.Action("SomeAction", "SomeController")',
    data: { 
        fileID: rightClickedFileId
    },
    success: function (data) {

    }
 });

If i define the url in this way, browser tries to post data to

如果我以这种方式定义url,浏览器会尝试将数据发布到

http://localhost:5907/FileManager/@Url.Action(%22SomeAction%22,%20%22SomeController%22)

and as a result my ajax call fails.

结果我的ajax调用失败了。

However, If I use '/SomeController/SomeAction' instead, everythings works fine.

然而,如果我用‘/SomeController/SomeAction’来代替,一切都没问题。

Second works ok, but I am wondering the problem with first one ? Could it be due to routing configuration ?

第二种方法可以,但是我想知道第一种方法有什么问题吗?可能是由于路由配置吗?

Thanks.

谢谢。

4 个解决方案

#1


20  

Url.Action is an html helper method which will work in your razor view, not in your external javascript files.

Url。Action是一个html助手方法,它将在您的razor视图中工作,而不是在您的外部javascript文件中。

What you can do is, get the relative url to your action method using Url.Action helper method in a razor view and set that to a javascript variable and use that in your external js file. When doing this, Always make sure to use javascript namespacing to avoid possible conflict with existing global variables.

您可以做的是,使用url获取您的操作方法的相对url。在razor视图中设置一个动作助手方法,并将其设置为一个javascript变量,并在外部js文件中使用。在执行此操作时,始终确保使用javascript命名空间,以避免与现有全局变量发生冲突。

You may add this code in the _Layout.cshtml

您可以在_Layout.cshtml中添加此代码。

<script type="text/javascript">

    var yourApp = yourApp || {};
    yourApp.Urls = yourApp.Urls || {};
    yourApp.Urls.baseUrl = '@Url.Content("~")';
    yourApp.Urls.editUserUrl= '@Url.Action("Edit","User")';

</script>

Or in your page specific view,

或者在你的页面特定视图中,

@section Scripts
{
  <script type="text/javascript">

     var yourApp = yourApp || {};
     yourApp.Urls = yourApp.Urls || {};
     yourApp.Urls.baseUrl = '@Url.Content("~")';
     yourApp.Urls.editUserUrl= '@Url.Action("Edit","User")';

  </script>
  <script src="~/Scripts/PageSpecificExternalJsFile.js"></script>    
}

Now in your external javascript file, you can access it like

现在,在您的外部javascript文件中,您可以像这样访问它

var urlToEditUser = yourApp.Urls.editUserUrl;
//you can use urlToEditUser  now

// Or With the base url, you may safely add the remaining part of your url.
var urlToEditUser2 = yourApp.Urls.baseUrl+"User/Edit";
//you can use urlToEditUser2  now

Always use the Url.Action or Url.RouteUrl html helper methods to build the relative url to the action methods. It will take care of correctly building the url regardless of your current page/path.

总是使用Url。行动或Url。RouteUrl html helper方法,用于构建操作方法的相对url。它将负责正确地构建url,而不考虑当前的页面/路径。

If you want to do the same thing inside your angular controller's/data services etc, take a look at this post which explains how to use the angular value provider to do the same.

如果你想在你的角控制器/数据服务中做同样的事情,看看这篇文章,它解释了如何使用角值提供程序来做同样的事情。

#2


7  

You can't use '@Url.Action("SomeAction", "SomeController")' in js file, because this is ASP.NET MVC helper, if you put your code to the view everything will work.

你不能使用。js文件中的Action("SomeAction", "SomeController")',因为这是ASP。NET MVC助手,如果你把你的代码放到视图中,所有的东西都会工作。

#3


1  

My approach is similar to @Shyju's, except instead of setting one variable in the Razor view, I call an init function in my JavaScript, and pass it all the parameters that I need interpreted by Razor.

我的方法类似于@Shyju,除了在Razor视图中设置一个变量,我在JavaScript中调用一个init函数,并将所有需要用Razor解释的参数传递给它。

So the code would look like this:

代码是这样的

MyScript.init({
    editUserUrl: "@Url.Action("Edit","User")",
    anotherUrl: "@Url.Action("AnotherUrl", "User")"
})

Then in JavaScript:

然后在JavaScript中:

var m_options;
MyScript.init = function(options) {
    m_options = options;
}

// use with m_options.editUserUrl here
$.ajax(
{
    type: "POST",
    url: m_options.editUserUrl,
    data: { 
        fileID: rightClickedFileId
    },
    success: function (data) {
    }
 });

I wrote a more detailed post about it here: http://blog.blanklabs.com/2015/02/aspnet-mvc-refactoring-friendly.html

我在这里写了一篇更详细的文章:http://blog.com/2015/02/aspnet -mvc-refactoring-friendly.html。

#4


1  

you can write it like this way just

你可以这样写

url:'/controllername/Actionname',

#1


20  

Url.Action is an html helper method which will work in your razor view, not in your external javascript files.

Url。Action是一个html助手方法,它将在您的razor视图中工作,而不是在您的外部javascript文件中。

What you can do is, get the relative url to your action method using Url.Action helper method in a razor view and set that to a javascript variable and use that in your external js file. When doing this, Always make sure to use javascript namespacing to avoid possible conflict with existing global variables.

您可以做的是,使用url获取您的操作方法的相对url。在razor视图中设置一个动作助手方法,并将其设置为一个javascript变量,并在外部js文件中使用。在执行此操作时,始终确保使用javascript命名空间,以避免与现有全局变量发生冲突。

You may add this code in the _Layout.cshtml

您可以在_Layout.cshtml中添加此代码。

<script type="text/javascript">

    var yourApp = yourApp || {};
    yourApp.Urls = yourApp.Urls || {};
    yourApp.Urls.baseUrl = '@Url.Content("~")';
    yourApp.Urls.editUserUrl= '@Url.Action("Edit","User")';

</script>

Or in your page specific view,

或者在你的页面特定视图中,

@section Scripts
{
  <script type="text/javascript">

     var yourApp = yourApp || {};
     yourApp.Urls = yourApp.Urls || {};
     yourApp.Urls.baseUrl = '@Url.Content("~")';
     yourApp.Urls.editUserUrl= '@Url.Action("Edit","User")';

  </script>
  <script src="~/Scripts/PageSpecificExternalJsFile.js"></script>    
}

Now in your external javascript file, you can access it like

现在,在您的外部javascript文件中,您可以像这样访问它

var urlToEditUser = yourApp.Urls.editUserUrl;
//you can use urlToEditUser  now

// Or With the base url, you may safely add the remaining part of your url.
var urlToEditUser2 = yourApp.Urls.baseUrl+"User/Edit";
//you can use urlToEditUser2  now

Always use the Url.Action or Url.RouteUrl html helper methods to build the relative url to the action methods. It will take care of correctly building the url regardless of your current page/path.

总是使用Url。行动或Url。RouteUrl html helper方法,用于构建操作方法的相对url。它将负责正确地构建url,而不考虑当前的页面/路径。

If you want to do the same thing inside your angular controller's/data services etc, take a look at this post which explains how to use the angular value provider to do the same.

如果你想在你的角控制器/数据服务中做同样的事情,看看这篇文章,它解释了如何使用角值提供程序来做同样的事情。

#2


7  

You can't use '@Url.Action("SomeAction", "SomeController")' in js file, because this is ASP.NET MVC helper, if you put your code to the view everything will work.

你不能使用。js文件中的Action("SomeAction", "SomeController")',因为这是ASP。NET MVC助手,如果你把你的代码放到视图中,所有的东西都会工作。

#3


1  

My approach is similar to @Shyju's, except instead of setting one variable in the Razor view, I call an init function in my JavaScript, and pass it all the parameters that I need interpreted by Razor.

我的方法类似于@Shyju,除了在Razor视图中设置一个变量,我在JavaScript中调用一个init函数,并将所有需要用Razor解释的参数传递给它。

So the code would look like this:

代码是这样的

MyScript.init({
    editUserUrl: "@Url.Action("Edit","User")",
    anotherUrl: "@Url.Action("AnotherUrl", "User")"
})

Then in JavaScript:

然后在JavaScript中:

var m_options;
MyScript.init = function(options) {
    m_options = options;
}

// use with m_options.editUserUrl here
$.ajax(
{
    type: "POST",
    url: m_options.editUserUrl,
    data: { 
        fileID: rightClickedFileId
    },
    success: function (data) {
    }
 });

I wrote a more detailed post about it here: http://blog.blanklabs.com/2015/02/aspnet-mvc-refactoring-friendly.html

我在这里写了一篇更详细的文章:http://blog.com/2015/02/aspnet -mvc-refactoring-friendly.html。

#4


1  

you can write it like this way just

你可以这样写

url:'/controllername/Actionname',