简体   繁体   English

ASP.NET Web Api 路由在 ZD7EFA19FBE7D3972745ADB602Z223 中不起作用

[英]ASP.NET Web Api Routing not working in C#

I have a GET-endpoint called GetPostalCodeXml but it doesn't seem to ever get routed correctly我有一个名为GetPostalCodeXml的 GET 端点,但它似乎从未正确路由

The method is in a class called WebServiceController like so:该方法位于名为WebServiceController的 class 中,如下所示:

public class WebServiceController : ApiController
{
    [HttpGet]
    [ActionName("GetPostalCodeXml")]
    public HttpResponseMessage GetPostalCodeXml(string postalCode)
    {
        return Request.CreateResponse(HttpStatusCode.OK, "ok");
    }
}

In my WebApiConfig.cs file the following code is run:在我的WebApiConfig.cs文件中,运行以下代码:

        config.Routes.MapHttpRoute(
            name: "GetPostalCodeXml",
            routeTemplate: "WebService/GetPostalCodeXml",
            defaults: new { controller = "WebService", action = "GetPostalCodeXml" }
        );

I'm trying to make a GET-request to the endpoint with the following URL:我正在尝试使用以下 URL 向端点发出 GET 请求:
http://localhost:60736/WebService/GetPostalCodeXml?postalCode=73522 http://localhost:60736/WebService/GetPostalCodeXml?postalCode=73522
but it doesn't seem to ever work.但它似乎从来没有工作过。

I have tried writing the route in different ways and changing the method name among other things.我尝试过以不同的方式编写路线并更改方法名称等。 I have also tried reading on the Microsoft docs page on routing: https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api我还尝试阅读有关路由的 Microsoft 文档页面: https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet -web-api
I still do not understand what is wrong or what I can do to solve it.我仍然不明白出了什么问题或我能做些什么来解决它。 I feel like I am misunderstanding something basic.我觉得我误解了一些基本的东西。 Please help请帮忙

I feel pretty stupid now but the problem was me.我现在觉得很愚蠢,但问题出在我身上。 I was making requests to the wrong port in my local IIS.我向本地 IIS 中的错误端口发出请求。 Not that it matters to anyone else but the correct port was 60744 and not 60736. The routing was correct all along.对其他人来说并不重要,但正确的端口是 60744 而不是 60736。路由一直都是正确的。

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

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