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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12514745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:47:07  来源:igfitidea点击:

ASP.NET MVC Url.Action in JQuery is not recognized

jqueryajaxasp.net-mvc

提问by emre nevayeshirazi

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.

谢谢。

回答by Shyju

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

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

What you can do is, get the relative url to your action method using Url.Actionhelper 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 namespacingto avoid possible conflict with existing global variables.

您可以做的是,Url.Action在 razor 视图中使用helper 方法获取您的操作方法的相对 url,并将其设置为 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.Actionor Url.RouteUrlhtml 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.ActionUrl.RouteUrlhtml 辅助方法来构建动作方法的相对 url。无论您当前的页面/路径如何,它都会负责正确构建 url。

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

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

回答by webdeveloper

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.

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

回答by Daniel Gabriel

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

我的方法类似于@Shyju 的方法,除了不是在 Razor 视图中设置一个变量,而是init在我的 JavaScript 中调用一个函数,并将我需要由 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

回答by Rakib Ahmed

you can write it like this way just

你可以这样写

url:'/controllername/Actionname',