简体   繁体   中英

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

The method is in a class called WebServiceController like so:

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:

        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:
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
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. Not that it matters to anyone else but the correct port was 60744 and not 60736. The routing was correct all along.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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