【问题标题】:Using AJAX with ASP.NET MVC 1.0 on IIS 6在 IIS 6 上使用 AJAX 和 ASP.NET MVC 1.0
【发布时间】:2009-12-29 15:33:58
【问题描述】:

我在 IIS 6.0 上尝试将 AJAX 和 jQuery 与 ASP.NET MVC 一起使用时遇到了问题。当我尝试通过 jQuery 调用操作时收到 403.1 错误。为了支持这一点,我必须在 web.config 中添加什么内容吗?

客户端代码

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>

<script src="../../Scripts/jquery-1.3.2.js" type="text/javascript"></script>

<script type="text/javascript">
    function deleteRecord(recordId) {
        // Perform delete
        $.ajax(
        {
            type: "DELETE",
            url: "/Financial.mvc/DeleteSibling/" + recordId,
            data: "{}",
            success: function(result) {
                window.location.reload();
            },
            error: function(req, status, error) {
                alert("Unable to delete record.");
            }
        });
    }


</script>

<a onclick="deleteRecord(<%= sibling.Id %>)" href="JavaScript:void(0)">Delete</a>

服务器代码

[AcceptVerbs(HttpVerbs.Delete)]
public virtual ActionResult DeleteSibling(int id)
{
    var sibling = this.siblingRepository.Retrieve(id);
    if (sibling != null)
    {
        this.siblingRepository.Delete(sibling);
        this.siblingRepository.SubmitChanges();
    }

    return RedirectToAction(this.Actions.Siblings);
}

错误

您试图从不允许执行程序的目录中执行 CGI、ISAPI 或其他可执行程序。

HTTP 错误 403.1 - 禁止:执行访问被拒绝。 互联网信息服务 (IIS)


更新

Darin 正确地指出,如果您将 DELETE 动词添加到 .mvc 扩展名,它会有所帮助,但是我现在遇到了以下问题:

[HttpException (0x80004005): 路径'DELETE'被禁止。] System.Web.HttpMethodNotAllowedHandler.ProcessRequest(HttpContext context) +80 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute() +179 System .Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

状态:405 - 方法不允许

【问题讨论】:

    标签: asp.net-mvc iis-6 jquery


    【解决方案1】:

    当您在 IIS 中使用 aspnet_isapi.dll 注册 .mvc 扩展时,您需要启用 DELETE 动词:

    alt text http://support.citrix.com/article/html/images/CTX104183-1.gif

    【讨论】:

    • 我输入了以下动词:GET,HEAD,POST,DEBUG 我还取消选中“验证该文件是否存在”。
    • 取消选中Verify that file exists并将DELETE动词添加到列表中或选中All verbs
    • 谢谢 - 一旦你指出这一点就很明显了......现在我得到一个 HTTPException 抛出:[HttpException(0x80004005):路径'DELETE'被禁止。] System.Web.HttpMethodNotAllowedHandler.ProcessRequest (HttpContext上下文) +80 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute() +179 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +87
    【解决方案2】:

    这是如何在代码中更改它:

    class IISDirEntry
        {    
    public void SetProperty(string metabasePath, string propertyName, string newValue)
            {
                //  metabasePath is of the form "IIS://servername/path"  
                try
                {
                    DirectoryEntry path = new DirectoryEntry(metabasePath);
                    PropertyValueCollection propValues = path.Properties[propertyName];
                    object[] propv = ((object[])propValues.Value);
                    int searchIndex = newValue.IndexOf(',');
    
                    int index = -1;                
                    for (int i = 0; i < propv.Length; i++)
                    {
                        if (propv[i].ToString().ToLower().StartsWith(newValue.ToLower().Substring(0, searchIndex + 1)))
                        {
                            index = i;
                            break;
                        }
                    }
                    if (index != -1)
                    {
                        propv[index] = newValue;
                    }
                    else
                    {
                        List<object> proplist = new List<object>(propv);
                        proplist.Add(newValue);
                        propv = proplist.ToArray();
                    }
    
                    path.Properties[propertyName].Value = propv;
                    path.CommitChanges();
                    Console.WriteLine("IIS6 Verbs fixed.");
                }
                catch (Exception ex)
                {
                    if ("HRESULT 0x80005006" == ex.Message)
                        Console.WriteLine(" Property {0} does not exist at {1}", propertyName, metabasePath);
                    else
                        Console.WriteLine("Failed in SetProperty with the following exception: \n{0}", ex.Message);
                }
            }
    }
    
        public void ChangeIIS6Verbs()
                {
                    if (IISVersion < 7.0)
                    {
                        IISDirEntry iisDirEntry = new IISDirEntry();
                        string windir = Environment.GetEnvironmentVariable("windir");
                        iisDirEntry.SetProperty("IIS://localhost/W3SVC/" + SiteIndex + "/ROOT", "ScriptMaps",
                        @".aspx," + Path.Combine(windir, @"\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll") + ",1,GET,HEAD,POST,DEBUG,DELETE");
                    }
                }
    

    在安装时需要配置时很有用

    【讨论】:

      猜你喜欢
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 2010-09-12
      相关资源
      最近更新 更多