c# - 当返回类型为 HttpResponseMessage 时,Web Api Get() 路由不起作用

标签 c# asp.net-mvc asp.net-web-api asp.net-mvc-routing

这让我有点头疼。最终,我尝试返回图像,但我一直在尝试并将其简化为使用字符串。

我想要什么: 转到网址:

http://xxx/api/helloworld/1

回复:“Hello world!”

以下 Web API 声明适用于提供的 url;

public string Get([FromUri]int id) { return "Hello World"; }
public Task<string> Get([FromUri]int id) { return "Hello World"; }

什么不起作用;

public HttpResponseMessage Get([FromUri]int id) 
{ 
    return Request.CreateResponse<string>(HttpStatusCode.OK, "Hello World");
}

public HttpResponseMessage Get([FromUri]int id)
{
    HttpResponseMessage response = new HttpResponseMessage();

    string text = "Hello World";
    MemoryStream test = new MemoryStream();
    test.Write(ConversionUtilities.ToBytes(text) /*custom string->byte[] method, UTF-8 encoding*/, 0, text.Length);

    response.Content = new StreamContent(test);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
    response.StatusCode = HttpStatusCode.OK;

    return response;
}

当我使用返回类型为 HttpResponseMessage 的 Get() 时,会发生以下错误;

No HTTP resource was found that matches the request URI "http://xxx/api/helloworld/1"

此错误仅针对此特定返回类型出现。现在,我在 WebApiConfig.cs 文件中的路由如下(适用于“字符串”返回类型);

// Controller with ID
// To handle routes like "/api/VTRouting/1"
config.Routes.MapHttpRoute(
    name: "ControllerAndId",
    routeTemplate: "api/{controller}/{id}",
    defaults: null,
    constraints: new { id = @"^\d+$" } // Only integers 
);

// Controllers with Actions
// To handle routes like "/api/VTRouting/route"
config.Routes.MapHttpRoute(
    name: "ControllerAndAction",
    routeTemplate: "api/{controller}/{action}"
);

// Controller Only
// To handle routes like "/api/VTRouting"
config.Routes.MapHttpRoute(
    name: "ControllerOnly",
    routeTemplate: "api/{controller}"
);

有什么想法吗?我对返回类型行为感到困惑:-S

最佳答案

找到原因了!我根据有效的模板创建了一个空白的 api Controller 。提示进行一些复制/粘贴以缩小原因范围。这很微妙,我发布的代码有效,因为我更改了公开发布的变量名称 - 这最终是问题的原因。想想吧。

要复制,请按照正常方式创建模板。这将创建一个方法;

public string Get(int id)

将其更改为;

public string Get(int personID)

并尝试运行。您将收到如上所述的错误。看来 Get/Post/etc 参数的声明必须与路由中指定的相匹配。如果您像我一样将参数更改为“personID”,则可以通过将参数重命名回默认“id”或使用更新名称修改路由来修复;

config.Routes.MapHttpRoute(
    name: "ControllerAndId",
    routeTemplate: "api/{controller}/{personID}",
    defaults: null,
    constraints: new { personID = @"^\d+$" } // Only integers 
);

注意routeTemplate 和约束参数中的“{personID}”。该字段的名称必须与您的参数名称匹配。当查找如何下载文件或只是一般查看 Web api 时,它实际上从未在文档中说过这一点。当深入了解路由/MVC 的详细信息时可能会这样,但我不得不说,这很容易让一个不熟悉的人绊倒。请注意,我更有经验的同事也没有发现这一点:-)。我希望这可以帮助其他有同样痛苦的人!

关于c# - 当返回类型为 HttpResponseMessage 时,Web Api Get() 路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14857370/

相关文章:

c# - IEnumerable 到 IReadOnlyCollection

c# - 发送的电子邮件仍然包含 HTML 标签。

c# - For 循环,应用程序挂起

javascript - 我需要在我的 HTML 中转义什么(JSON 响应)

jquery - 我们如何通过Jquery从MVC中的FormData映射c#复杂列表对象

.net - Visual Studio 2010 中的 ASP.NET Web API 帮助页面

javascript - 防止 html onclick、onmouseout 中的 xss,...用于 SPA

c# - 根据父节点的属性获取子节点

c# - 在程序集中嵌入 Global.asax

asp.net-web-api - ASP.net 5 中的 IApplicationBuilder.UseJwtBearerAuthentication 扩展在哪里?