简体   繁体   English

GET方法不允许使用405方法

[英]Getting 405 Method Not Allowed on a GET

Clicking the following link in my webapp produces a 405: 在我的Web应用程序中单击以下链接将产生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. 当我F5时它可以工作,但是当我将它托管在IIS(7.5和6)中时,出现错误。

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 ?! 还有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. 每当我看到有人使用ViewData我都会不得不说。

What is the actual rendered HTML href in the anchor? 锚中实际呈现的HTML href是什么? Is the generated URI valid? 生成的URI有效吗?

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. 如果您的emailUidhash有一个句点,则需要对其进行编码。 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. 在将这些值连接到URL之前,对它们使用HttpUtility.UrlEncode

Why the 405? 为什么是405? Likely because there is no script mappings in IIS6 for the presumed "file extension". 可能是因为IIS6中对于假定的“文件扩展名”没有脚本映射。 It's not a file extension at all, but without encoding the period in the URL it doesn't know any better. 它根本不是文件扩展名,但是如果不对URL中的句点进行编码,那就更好了。

Well, I found my issue. 好吧,我发现了我的问题。 It turns out to be a problem with my IIS configuration. 事实证明我的IIS配置有问题。 In IIS 6, I didn't allow scripts to be executed in the directory, so my .svc would never run for my service host. 在IIS 6中,我不允许在目录中执行脚本,因此.svc永远不会为我的服务主机运行。 Once I changed that setting, I no longer received the 405 error... 更改该设置后,我不再收到405错误...

Many thanks to the other answers. 非常感谢其他答案。 I'll be sure to use the Html helper. 我一定会使用HTML帮助程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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