简体   繁体   中英

Getting 405 Method Not Allowed on a GET

Clicking the following link in my webapp produces a 405:

<a href="/unsubscribe/confirm?emailUid=<%= ViewData["emailUid"] %>&hash=<%= ViewData["hash"] %>" class="button">Yes, unsubscribe me.</a>

Here's the Confirm Action being called from the Unsubscribe Controller:

    [HttpGet]
    public ActionResult Confirm( string emailUid, string hash)
    {
        using ( UnsubscribeClient client = new UnsubscribeClient() )
        {
            UnsubscribeResponse response = client.Unsubscribe( emailUid, hash );
            return View( response );
        }

    }

Simple stuff. It works when I F5 it, but when I host it in IIS (both 7.5 and 6), I get the error.

I'm pretty lost with this. I've scoured the net, but can't seem to find any reason as to why this is happening.

<%= Html.ActionLink(
    "Yes, unsubscribe me.",
    "Confirm", 
    "Unsubscribe", 
    new { emailUid = ViewData["emailUid"], hash = ViewData["hash"] }, 
    new { @class = "button" }
) %>

Also ViewData ?! Please, remove this as well in favor of strongly typed views and view models. Everytime I see someone using ViewData I feel obliged to say that.

What is the actual rendered HTML href in the anchor? Is the generated URI valid?

Does changing it to this work:

 <%= Html.ActionLink("Yes, unsubscribe me.", "Confirm", "Unsubscribe", new { emailUid= ViewData["emailUid"], hash = ViewData["hash"]})%>

请确保输出的网址有效,并且可以正确转义任何无效字符,例如,请确保&&amp;转义&amp;

If there is a period in your emailUid or hash , it needs to be encoded. That's not the only problematic character, but I suspect it is the case here. Use HttpUtility.UrlEncode on these values before concatenating them into the URL.

Why the 405? Likely because there is no script mappings in IIS6 for the presumed "file extension". It's not a file extension at all, but without encoding the period in the URL it doesn't know any better.

Well, I found my issue. It turns out to be a problem with my IIS configuration. In IIS 6, I didn't allow scripts to be executed in the directory, so my .svc would never run for my service host. Once I changed that setting, I no longer received the 405 error...

Many thanks to the other answers. I'll be sure to use the Html helper.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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