views:

505

answers:

2

I am running into a problem trying to use AJAX and jQuery with ASP.NET MVC on IIS 6.0. I receive a 403.1 error when I attempt to invoke an action via jQuery. Is there anything I must add to the web.config in order to support this?

Client Code

<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>

Server Code

[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);
}

Error

You have attempted to execute a CGI, ISAPI, or other executable program from a directory that does not allow programs to be executed.

HTTP Error 403.1 - Forbidden: Execute access is denied. Internet Information Services (IIS)


Update

Darin correctly pointend out that it helps if you add the DELETE verb to .mvc extension, however I an now running into the following issue:

[HttpException (0x80004005): Path 'DELETE' is forbidden.] System.Web.HttpMethodNotAllowedHandler.ProcessRequest(HttpContext context) +80 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute() +179 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Status: 405 - Method not allowed

+3  A: 

When you register the .mvc extension with aspnet_isapi.dll in IIS you need to enable the DELETE verb:

alt text

Darin Dimitrov
I enterd the following verbs:GET,HEAD,POST,DEBUGI also unchecked "verify that file exists".
DarenMay
Uncheck `Verify that file exists` and add the `DELETE` verb to the list or check `All verbs`.
Darin Dimitrov
DarenMay
+1  A: 

This is how to change this in code:

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");
                }
            }

Useful if need to configure on install

Lion_cl