404 Not Found Ajax jquery MVC

Ade*_*gun 3 c# ajax asp.net-mvc jquery razor

我在Visual Studio 2013中创建了一个MVC3项目.视图引擎是razor.First我在@section Scripts {}视图中编写jquery.ajax(cshtml)它运行正常.但我在.js文件和调试中分离脚本我得到错误:(调试IIS Express)

IIS 8.0详细错误 - 404.0 - 未找到...更多信息:此错误表示服务器上不存在该文件或目录.创建文件或目录并再次尝试请求.
..

我的jquery函数(archive.js):

jQuery.AjaxGetProjects = function (dropdownId, detailDropDownId, authorized) {

var projectId = $(dropdownId).val();
if (projectId != null && projectId != '') {
    var url =
    $.ajax({
        type: "POST",
        url: '@Url.Action("GetProjects", "Archive")',
        data: {
            'projectId': projectId,
            'authorized': authorized
        },
        success: function (departman) {
            var length = 0;
            $(detailDropDownId).empty();
            $.each(departman, function (index, proje) {
                length = length + 1;
                $(detailDropDownId).append($('<option/>', {
                    value: proje.Value,
                    text: proje.Text,
                    selected: proje.Selected
                }));
            });
            if (length == 2) {
                $(detailDropDownId).trigger('change');
            }

        },
        error: function (xhr, ajaxOptions, thrownError) {
            // bu k?s?mda e?er ajax i?lemi ba?ar?s?z ise
            // hata mesaj? verebiliriz.
            alert(xhr.responseText);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

}

控制器:

public class ArchiveController : BaseController
{
   ...
   public ActionResult GetProjects(int projectId, bool authorized)
    {

        IArchive arch = WcfServiceHandler.GetDmsService<IArchive>();

        List<Poco> list = arch.GetProjects(UserManager.GetUserInfo(), projectId);
        var t = MvcHelper.GetDropDownList<Poco>(list, "NAME", "ID", "");
        return Json(t, JsonRequestBehavior.AllowGet);
    }
Run Code Online (Sandbox Code Playgroud)

我试过这个但不行(我收到一个新错误:无法找到资源):

  <system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
Run Code Online (Sandbox Code Playgroud)

请帮忙..

mat*_*mmo 5

您不能在JavaScript文件中使用Razor(除非您使用类似RazorJS扩展名的内容).如果你在浏览器开发工具中查看你的JS,你会看到输出的网址是:

url: '@Url.Action("GetProjects", "Archive")',
Run Code Online (Sandbox Code Playgroud)

它应该是:

url: www.yoursite.com/Archive/GetProjects
Run Code Online (Sandbox Code Playgroud)

要修复它,您可以从视图到该JS进行方法调用并传入URL,例如:

视图:

<script>
    callMethod('@Url.Action("GetProjects", "Archive")');
</script>
Run Code Online (Sandbox Code Playgroud)

然后你的JS将是:

function callMethod(url) {
   ....
Run Code Online (Sandbox Code Playgroud)

或者失败了,你将硬编码编码为:

url: '/Archive/GetProjects'
Run Code Online (Sandbox Code Playgroud)

但是如果要部署到子目录中的站点,请小心,该链接无效.