在ASP.NET MVC中,我们有@Url.Action行动.有类似的东西@Url.Api会路由到/ api/controller吗?
我使用ASP.NET Web API编写了REST服务.我正在尝试发送HttpDelete请求,但是我收到以下错误:
405 - 不允许用于访问此页面的HTTP动词
我认为我接近解决方案,我发现我应该启用IIS远程管理,转到Handler Mappings部分并将DELETE动词添加到适当的位置......但问题是有很多不同的位置列表......(就像这里:http://www.somacon.com/p126.php).
我应该编辑哪一个?其中很少没有扩展,例如"ExtensionUrlHandler-Integrated-4.0",我添加了DELETE动词,但它仍然不起作用......
修改那个只是在黑暗中拍摄,所以我应该修改不同的位置吗?如果是这样,哪一个?或者还有什么我应该做的?
相同的Web服务在我的本地服务上工作得很好,所以我猜问题是远程IIS ...
问候
我使用HTTP PUT和DELETE我的ASP.NET MVC3应用.当我在本地运行时,每件事都能正常工作; 但是当我将应用程序发布到服务器时,这些方法不起作用.
是否有任何特殊设置可以使Web服务器支持PUT和DELETE请求?我正在使用IIS 7.5的共享主机.
更新:
我启用PUT并DELETE请求IIS manager.PUT命令工作正常.但DELETE仍然行不通.我创建请求jQuery:
我在这个页面:
http://domain.com/dashboard/edit-site/103323/links/
Run Code Online (Sandbox Code Playgroud)
我的ajax电话是:
$.ajax({
// url: same as page-url,
cache: false,
type: 'DELETE',
data: { linkid: $(link).data("linkid") },
beforeSend: function () {
// doing something in UI
},
complete: function () {
// doing something in UI
},
success: function (data) {
// doing something in UI
},
error: function () { …Run Code Online (Sandbox Code Playgroud) 在我的Web API项目中,我无法执行HTTP PUT我的资源.我已经阅读了一些 关于这个问题的类似 问题 , 并且我遵循了建议的建议.
首先,我在我的机器上完全卸载了WebDAV(Windows 7 64位),然后重新启动了我的机器.
其次,WebDAV处理程序在我的指定中被删除,web.config并且HTTP PUT动词被指定为允许无扩展URL处理程序.
<modules runAllManagedModulesForAllRequests="false">
<remove name="WebDAVModule"/>
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="WebDAV"/>
<add name="ExtensionlessUrlHandler-Integrated-4.0"
path="*."
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
type="System.Web.Handlers.TransferRequestHandler"
resourceType="Unspecified"
requireAccess="Script"
preCondition="integratedMode,runtimeVersionv4.0" />
<add name="AttributeRouting" path="routes.axd" verb="*" type="AttributeRouting.Web.Logging.LogRoutesHandler, AttributeRouting.Web" />
</handlers>
Run Code Online (Sandbox Code Playgroud)
我甚至尝试添加ISAPI无扩展URL处理程序(32位和64位)并将我的应用程序从集成管道应用程序池更改为经典应用程序池.
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit"
path="*."
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
modules="IsapiModule"
scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll"
preCondition="classicMode,runtimeVersionv4.0,bitness32"
responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit"
path="*."
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
modules="IsapiModule"
scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll"
preCondition="classicMode,runtimeVersionv4.0,bitness64"
responseBufferLimit="0" />
Run Code Online (Sandbox Code Playgroud)
我目前正在使用Thinktecture IdentityModel来启用跨源资源共享(CORS)支持.为了我的理智,我已经选择了启用一切以确保HTTP …
在通过ASP.NET Web API提交请求时,我很难在我的Controller上获取DELETE方法.它返回404,但我无法弄清楚原因.GET和POST请求按预期工作,返回项目列表以及提供id时的单个项目,但是当我使用DELETE请求调用API时,我得到404错误.
场景:
虽然我已经安装了MVC4软件包以利用Web API功能,但它不是MVC应用程序.
RouteTable.Routes.MapHttpRoute(
"Default",
"api/{controller}/{id}",
new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
public HttpResponseMessage<Customer> Post(Customer customer)
{
CustomerDb.Customers.AddObject(customer);
CustomerDb.SaveChanges();
var response = new HttpResponseMessage<Customer>(customer, HttpStatusCode.Created);
response.Headers.Location = new Uri(Request.RequestUri, "/api/Customer/"+customer.id.ToString());
return response;
}
public CustomerDTO Get(int id)
{
CustomerDTO custDTO = null;
Customer cust = CustomerDb.Customers.Where(c => c.id == id).SingleOrDefault();
if (cust == null)
throw new HttpResponseException(HttpStatusCode.BadRequest);
else
custDTO = new CustomerDTO(cust);
return custDTO;
}
public IEnumerable<CustomerDTO> Get()
{
IQueryable<Customer> …Run Code Online (Sandbox Code Playgroud) 尽管DELETE请求在本地成功运行,但在将应用程序部署到MS Azure时它不起作用.
这是我在尝试删除记录时在控制台中获得的内容:
DELETE http://mywebsite.azurewebsites.net/api/subjects/1 405 (Method Not Allowed)
Run Code Online (Sandbox Code Playgroud)
我的应用程序使用mySQL的cleardb
如何解决这样的问题?
编辑
我的应用程序是一个PHP 5.5应用程序,这是我尝试web.config启用DELETE和PUT,但它不起作用:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<security>
<requestFiltering>
<verbs applyToWebDAV="false">
<add verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS,XYZ" allowed="true" />
</verbs>
</requestFiltering>
</security>
<handlers>
<remove …Run Code Online (Sandbox Code Playgroud) 在Visual Studio 2013下向本地IIS Express网站发出完美的动词时:
CROMULENT http://localhost:7579/Handler.ashx HTTP/1.1
Host: localhost:7579
Run Code Online (Sandbox Code Playgroud)
服务器响应错误:
HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE
Run Code Online (Sandbox Code Playgroud)
这是对"通用处理程序" (即.ashx)的请求.如果我再次尝试静态资源:
SCHWIFTY http://localhost:7579/Default.htm HTTP/1.1
Host: localhost:7579
Run Code Online (Sandbox Code Playgroud)
服务器响应错误:
HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE
Run Code Online (Sandbox Code Playgroud)
这是尝试使用HTTP谓词的所有方法:
DELETE http://localhost:7579/Handler.ashx HTTP/1.1
Host: localhost:7579
HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE
PUThttp://localhost:7579/Handler.ashx HTTP/1.1
Host: localhost:7579
HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE
Run Code Online (Sandbox Code Playgroud)
这个问题有时被我要求死亡.但没有人提出过解决方案.
微软的Bug
从根本上说,问题在于Microsoft默认情况下会破坏IIS和IISExpress.他们不需要像处理Web服务器那样处理HTTP谓词,而是处理动词.
在管理在Windows Server上运行的完整IIS时,最容易看到这一点.挑选任何内置的处理程序(例如CSHTML处理程序)的,你可以看到,有人认为这将是 …
我正在尝试在MVC 4应用程序中使用PUT,我收到405错误.
在我的路由中,我对允许PUT和POST的路由有约束,POST到端点工作,PUT失败了405.
我已经按照这里的建议ASP.NET Web API仅在某些服务器上返回404 for PUT,这里ASP.NET MVC在HTTP DELETE请求上得到405错误?
我也从IIS中删除了WeDAV,但我仍然得到了405.任何人还有其他建议吗?
我在IIS 8(使用Visual Studio 2012)上也遇到了完全相同的问题,因为我遵循了这个建议ASP.NET Web API - PUT&DELETE动词不允许 - IIS 8仍然没有运气
我几乎遍历了所有我能找到的关于SO等的文章。我已经对进行了更改以Web.config符合答案,因为它们似乎都指向删除WebDAV模块和处理程序。
但是,我仍然收到错误:
405不允许的方法
请求的资源不支持http方法“ PUT”。
注意:这最初只是一个MVC 4项目。我添加了工件以支持Web API。好像我可能错过了一些东西。
注意:GET从Angular进行的调用工作正常。
configuration.Routes.MapHttpRoute(
name: "Default API",
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "Get", id = RouteParameter.Optional },
constraints: new { id = @"[\da-z-]{36}" });
configuration.Routes.MapHttpRoute(
name: "Default API with Action",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = "Get" });
var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
formatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
Run Code Online (Sandbox Code Playgroud)
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<handlers accessPolicy="Read, Execute, Script">
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" …Run Code Online (Sandbox Code Playgroud) 我检查了一切,没有一个配置工作正常.
THE GET,POST正在工作但是我得到了405消息.我没有WebDEV.
这是我的配置.
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV"/>
<remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
<remove name="OPTIONSVerbHandler"/>
<remove name="TRACEVerbHandler"/>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
它让我疯了!
我已经提到了所有可用的建议,例如: Asp.NET Web API - 405 - 不允许用于访问此页面的HTTP动词 - 如何设置处理程序映射

即使我使用Basic Auth/Windows Auth.禁用它也没有区别.
[System.Web.HttpHttpPut] public override HttpResponseMessage Put(int id,Request entity){....}
注意:我刚刚安装了登台服务器,它已经运行了.但是,它不能在我的机器上工作......

我有一个ASP.NET MVC 5控制器使用属性路由,似乎没有使用DELETE操作.
[Route("deletealbum/{id}")]
[AcceptVerbs(HttpVerbs.Delete)]
public ActionResult DeleteAlbum(int id)
{
// rest of the code omitted here
return Json(true, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
我明确地将路由名称更改为deletename以排除其他路由过载冲突.
上面的操作没有触发 - 我从IIS中找不到404.但是,如果我将上面的动作切换到HttpVerbs.Get路线是没有问题的.
长期以来第一次使用MVC而不是Web API构建API,我无法弄清楚为什么路由没有触发 - 它几乎看起来像任何DELETE操作都被阻止了.我在不同的控制器中看到相同的行为 - GET工作正常,但是DELETE操作返回404.是否需要翻转某些配置设置以启用其他动词?
我尝试在我的网络项目上使用 REST。POST 有效,但 DELETE 和 PUT 不起作用,我会看到错误:HTTP 状态 405 - 不允许方法。并且 GET 根本不起作用:
“‘id’:RFC 2068 中未定义,且 Servlet API 不支持。描述:服务器不支持满足此请求所需的功能。”
这是我的代码:
package rest;
import domain.model.Client;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.*;
import javax.xml.bind.annotation.XmlRootElement;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
@XmlRootElement
@Path("/clients")
@Stateless
public class ClientResources {
@PersistenceContext
EntityManager entityManager;
@GET
@Consumes(MediaType.APPLICATION_JSON)
public Response getAll() {
List<Client> matchHistories = new ArrayList<>();
for (Client m : entityManager
.createNamedQuery("client.all", Client.class)
.getResultList())
matchHistories.add(m);
return Response.ok(new GenericEntity<List<Client>>(matchHistories) {
}).build();
} …Run Code Online (Sandbox Code Playgroud) DELETE 方法在 IIS 10 中显示以下错误
请求的资源上不存在“Access-Control-Allow-Origin”标头。
所有其他方法(如 POST 和 GET)都可以正常工作。此外,DELETE 在开发时在 iis express 中运行良好。我为 WebApiConfig 类中的所有请求启用了 cors
public static void Register(HttpConfiguration config)
{
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
我的网络配置看起来像这样
<?xml version="1.0" encoding="utf-8"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=301879 --> <configuration> <configSections>
<!-- For more information on Entity Framework …Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×4
iis ×4
iis-7.5 ×4
c# ×3
asp.net ×2
http-delete ×2
http-put ×2
iis-7 ×2
angularjs ×1
cleardb ×1
cors ×1
glassfish ×1
http ×1
httprequest ×1
iis-10 ×1
iis-8 ×1
iis-express ×1
jquery ×1
php ×1
rest ×1