【发布时间】:2014-03-01 04:19:46
【问题描述】:
我已经成功使用这个方法来获取 REST 数据了:
private JArray GetRESTData(string uri)
{
try
{
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
var reader = new StreamReader(webResponse.GetResponseStream());
string s = reader.ReadToEnd();
return JsonConvert.DeserializeObject<JArray>(s);
}
catch // This method crashes if only one json "record" is found - try this:
{
try
{
MessageBox.Show(GetScalarVal(uri));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return null;
}
在 webRequest 和 webResponse 分配之间,我添加了这个:
if (uri.Contains("Post"))
{
webRequest.Method = "POST";
}
...并用这个 URI 调用它:
http://localhost:28642/api/Departments/PostDepartment/42/76TrombonesLedTheZeppelin
虽然我有一个与之对应的 Post 方法:
控制器
[Route("api/Departments/PostDepartment/{accountid}/{name}/{dbContext=03}")]
public void PostDepartment(string accountid, string name, string dbContext)
{
_deptsRepository.PostDepartment(accountid, name, dbContext);
}
存储库
public Department PostDepartment(string accountid, string name, string dbContext)
{
int maxId = departments.Max(d => d.Id);
// Add to the in-memory generic list:
var dept = new Department {Id = maxId + 1, AccountId = accountid, Name = name};
departments.Add(dept);
// Add to the "database":
AddRecordToMSAccess(dept.AccountId, dept.Name, dbContext);
return dept;
}
...失败,“远程服务器返回错误:(405) Method Not Allowed.”
为什么不允许?
更新
根据我在此处找到的内容:http://blog.codelab.co.nz/2013/04/29/405-method-not-allowed-using-asp-net-web-api/,我将其添加到 Web.Config:
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT"
type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
...所以它是从这个开始的:
<system.webServer>
<handlers>
<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></configuration>
...到这个:
<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="GET,HEAD,POST,DEBUG,PUT"
type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer></configuration>
....但没有区别。
更新 2
它甚至无法在 Controller 中调用:
[HttpPost]
[System.Web.Http.Route("api/Departments/PostDepartment/{accountid}/{name}/{dbContext=03}")]
public void PostDepartment(string accountid, string name, string dbContext)
{
_deptsRepository.PostDepartment(accountid, name, dbContext);
}
我有一个没有到达的断点...???
更新 3
VirtualBlackFox 的最后一条评论成功了。我只是改变了我的“这是一个帖子吗?”在我的客户端代码中如下:
if (uri.Contains("Post"))
{
webRequest.Method = "POST";
webRequest.ContentLength = 0; // <-- This line is all I added
}
...现在它可以工作了。
【问题讨论】:
-
您可能需要在您的
web.config中允许它,或者在api 方法允许POST中添加一个属性。不过我只使用过 WCF,所以不能肯定。 -
@CodeCaster:这就是为什么我添加了查找“Post”的逻辑——因为我想使用这种新型方法(到目前为止,它们都是“GET”。跨度>
-
其他 POST 方法是否有效,或者在其他请求时是否有效,例如通过 jQuery?您是否尝试过“web api 405”的众多结果?
-
@CodeCaster:由于 CORS 问题,我的 jQuery 测试还没有开始工作 (stackoverflow.com/questions/21561121/…)
标签: c# post json.net asp.net-web-api-routing http-status-code-405