简体   繁体   English

无法识别JQuery中的ASP.NET MVC Url.Action

[英]ASP.NET MVC Url.Action in JQuery is not recognized

I am trying to use Url.Action() method in my js file to define urls for my ajax calls. 我试图在我的js文件中使用Url.Action()方法来定义我的ajax调用的url。 So far I have failed. 到目前为止,我失败了。

 $.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 如果我以这种方式定义网址,浏览器会尝试将数据发布到

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' ,那么每一'/SomeController/SomeAction'都可以。

Second works ok, but I am wondering the problem with first one ? 第二个工作正常,但我想知道第一个问题? Could it be due to routing configuration ? 可能是由于路由配置?

Thanks. 谢谢。

Url.Action is an html helper method which will work in your razor view, not in your external javascript files. Url.Action是一个html帮助方法,它可以在你的剃刀视图中工作,而不是在外部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. 你可以做的是,在剃须刀视图中使用Url.Action帮助方法获取你的动作方法的相对url并将其设置为javascript变量并在外部js文件中使用它。 When doing this, Always make sure to use javascript namespacing to avoid possible conflict with existing global variables. 执行此操作时,请务必使用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. 始终使用Url.ActionUrl.RouteUrl html帮助程序方法来构建操作方法的相对URL。 It will take care of correctly building the url regardless of your current page/path. 无论您当前的页面/路径如何,它都会正确构建网址。

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. 如果你想在角度控制器/数据服务等内部做同样的事情,请看一下这篇文章 ,它解释了如何使用角度值提供者来做同样的事情。

您不能在js文件中使用'@Url.Action("SomeAction", "SomeController")' ,因为这是ASP.NET MVC帮助器,如果您将代码放到视图中,一切都会正常工作。

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.blanklabs.com/2015/02/aspnet-mvc-refactoring-friendly.html

你可以像这样写它

url:'/controllername/Actionname',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM